2013-09-25 68 views
0

我是Java的初學者,我正在編寫一個代碼來將json twitter數據集輸入到MYSQL數據庫中。我使用文件名「file」作爲輸入,解析它,並將其寫入SQL數據庫。不過,我不斷收到錯誤:在轉換爲MYSQL時,java中線程「main」java.lang.NullPointerException異常

異常線程 「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; 
    } 
} 
+1

什麼是line42? –

+0

第42行是這樣的:if(tweet.getLang()。equals(「en」)){ – elvirachrisanty

回答

0

嘗試

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

我認爲在某些推文中,如果用戶沒有設置它,語言可以是null

0

我遇到了使用Streaming API的相同問題......事實上,如果您的應用程序處理郵件太慢,您會斷開連接。解決方案是在郵件處理期間放棄檢查lang。

相反,你可以打開你的OAuth PARAMS語言過濾器...我使用劃線,這就是我的解決方案是這樣的:

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())); 
相關問題