【问题标题】:Exception in thread "main" java.lang.NullPointerException in java while converting to MYSQL转换为 MYSQL 时,java 中的线程“main”java.lang.NullPointerException 中的异常
【发布时间】:2013-09-25 08:12:55
【问题描述】:

我是 Java 初学者,我正在编写一个代码来将 json twitter 数据集输入到 MYSQL 数据库中。我使用文件名“file”作为输入,对其进行解析,并将其写入 SQL 数据库。但是,我不断收到错误消息:

线程“main”中的异常 java.lang.NullPointerException 在 edu.mbhs.twitter.test.main(test.java:42)

第 42 行包含:

  if (tweet.getLang().equals("en")){

这是我的主要课程代码:

public class test {

public static List <String> list = new ArrayList<String>();

public static void main(String[] args) throws FileNotFoundException, InterruptedException, SQLException {

    JSONParser j = new JSONParser(new File("file"));
    ArrayList<Tweet> tweets = j.getTweets();


   String sql = "insert into twitterdatasets (tweet_created_at, tweet_id, tweet_text, user_id, user_screen_name, user_location, user_followers_count, user_friends_count, user_created_at, user_verified, user_lang, tweet_retweeted_count, tweet_favorite_count, tweet_lang) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
   Connection conn = getConnection();
   PreparedStatement ps = conn.prepareStatement(sql);

   final int batchSize = 1000;
   int count = 0;

   for (Tweet tweet: tweets){

       if (tweet.getLang().equals("en")){
           User user = tweet.getUser();
           ps.setString(1, tweet.getCreatedAt());
           ps.setString(2, tweet.getIdStr());
           ps.setString(3, tweet.getText());
           ps.setString(4, user.getIdStr());
           ps.setString(5, user.getScreenName());
           ps.setString(6, user.getLocation());
           ps.setInt(7, user.getFollowersCount());
           ps.setInt(8, user.getFriendsCount());
           ps.setString(9, user.getCreatedAt());
           ps.setBoolean(10, user.getVerified());
           ps.setString(11, user.getLang());
           ps.setInt(12, tweet.getRetweetCount());
           ps.setInt(13, tweet.getFavoriteCount());
           ps.setString(14, tweet.getLang());
           ps.addBatch();
       if (++count % batchSize == 0){
           ps.executeBatch();
       }

       }
   }

   ps.executeBatch();
   ps.close();
   conn.close();
   System.out.println("File has been successfully written to database");
}

private static Connection getConnection() {
      Connection con = null;
      String url = "jdbc:mysql://localhost:3306/";
      String db =  "dbo";
      String driver = "com.mysql.jdbc.Driver";
      String user = "root";
      String pass = "elvira03";
      try {
       Class.forName(driver);
       con = DriverManager.getConnection(url + db, user, pass);
      } catch (ClassNotFoundException e) {
       e.printStackTrace();
      } catch (SQLException e) {
       e.printStackTrace();
      }
      return con;
     }
}

【问题讨论】:

  • 第 42 行是这样的:if (tweet.getLang().equals("en")){

标签: java mysql exception-handling nullpointerexception


【解决方案1】:

试试

if(tweet.getLang()!= null && tweet.getLang().equals("en") { ... }

如果用户没有设置,我认为在某些推文中语言可以是null

【讨论】:

    【解决方案2】:

    我在使用 Streaming API 时遇到了同样的问题...如果您的应用处理消息太慢,您会断开连接。解决方案是在消息处理期间放弃检查语言。

    相反,您可以在 OAuth 参数中打开语言过滤器...我正在使用 scribe,这就是我的解决方案的样子:

        OAuthService service = new ServiceBuilder().provider(TwitterApi.class).apiKey("your_twitter_api_key")
                    .apiSecret("your_twitter_api_secret").build();
        // Set your access token
        Token accessToken = new Token("your_twitter_token", "your_twitter_secret");
        // Let's generate the request
        System.out.println("Connecting to Twitter Public Stream");
        OAuthRequest request = new OAuthRequest(Verb.POST, STREAM_URI);
        request.addHeader("version", "HTTP/1.1");
        request.addHeader("host", "stream.twitter.com");
        request.setConnectionKeepAlive(true);
        request.addHeader("user-agent", "Twitter Stream Reader");
    
            // ADD YOUR LANGUAGE PREFS HERE...
            request.addBodyParameter("language", "en");
    
        service.signRequest(accessToken, request);
        Response response = request.send();
    
        // Create a reader to read Twitter's stream
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getStream()));
    

    【讨论】:

      猜你喜欢
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多