简体   繁体   English

JSON解析器-写入文本文件-线程“ main”中的异常java.lang.NullPointerException

[英]JSON parser - write to text file - Exception in thread “main” java.lang.NullPointerException

I am trying to write a list JSON that has been parsed into ArrayList String to a txt file. 我正在尝试将已解析为ArrayList字符串的列表JSON写入txt文件。 However I keep getting this error: 但是我不断收到此错误:

Exception in thread "main" java.lang.NullPointerException
    at kr.ac.uos.datamining.JSONParser.parseForTweets(JSONParser.java:47)
    at kr.ac.uos.datamining.JSONParser.<init>(JSONParser.java:25)
    at kr.ac.uos.datamining.test.main(test.java:21)

This is the main class code test.java: 这是主类代码test.java:

    package kr.ac.uos.datamining;

    import java.io.File; 
    import java.io.FileWriter;
    import java.io.FileNotFoundException; 
    import java.util.ArrayList;
    import java.util.List;
    import kr.ac.uos.datamining.JSONParser; 
    import kr.ac.uos.datamining.Tweet; 
    import kr.ac.uos.datamining.User;

    public class test {

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

    public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException, SQLException {
   **Line 21**
        JSONParser j = new JSONParser(new File("D:/usr/samsunggalaxy.txt"));
        ArrayList<Tweet> tweets = j.getTweets();

        for(Tweet tweet : tweets){
            User user = tweet.getUser();
            list.add(tweet.getText() + " | " + user.getScreenName());
        }

        FileWriter writer = new FileWriter("samsunggalaxy.txt");
        for(String str: list) {
            writer.write(str);
        }
        writer.close();
        }
    }

And this is the JSON Parser line of which the error trace shows: 这是错误跟踪显示的JSON Parser行:

    private void parseForTweets(){
            Scanner in = null;
            try {
                in = new Scanner(json);
            } catch (FileNotFoundException e) {

            }

**Line47**          while(in.hasNextLine()){
                String rawTweet = in.nextLine();

                if(!rawTweet.contains("\"retweeted\":true")){
                    Tweet tweet = TweetParser.parseTweet(rawTweet);
                    tweets.add(tweet);
                }
            }
        }

I think the error is in the line String rawTweet = in.nextLine(); 我认为错误是在一行String rawTweet = in.nextLine();中。 However I cant seem to figure this out. 但是我似乎无法弄清楚。 Is the JSON data itself has any influence on this? JSON数据本身对此有影响吗? I mean is it because I parsed the data wrongly? 我的意思是因为我错误地解析了数据吗? Or is it because of another reason? 还是因为其他原因?

Any help would be really appreciated 任何帮助将非常感激

It seems that the problem is that you're not handling the exception, therefore, if the new Scanner(json) line failed, the in reference would be null. 似乎问题在于您没有处理该异常,因此,如果new Scanner(json)行失败,则in引用将为null。 In such a case, you'd fail in the while(in.hasNextLine()) line. 在这种情况下,您将在while(in.hasNextLine())行中失败。

However, without line numbers in the code it's hard to tell exactly if that's really the problem. 但是,在代码中没有行号的情况下,很难确切地说出这是否是真正的问题。

if you are sure the problem is in.nextLine() ( please debug your code to be sure where is the problem) then try this one : 如果您确定问题出在in.nextLine()(请调试您的代码以确保问题出在哪里),然后尝试以下操作:

 Iterator<JSONObject> iterator = yourJsonObject.iterator();
 while (iterator.hasNext()) {
 JSONObject factObj = (JSONObject) iterator.next();
 ...

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常java.lang.NullPointerException - JSon to Java - Exception in thread “main” java.lang.NullPointerException - JSon to Java , 线程“main”中的异常 java.lang.NullPointerException - ,Exception in thread "main" java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException “线程“ main”中的异常java.lang.NullPointerException” - “Exception in thread ”main“ java.lang.NullPointerException” 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“main”中的异常 java.lang.NullPointerException 5 - Exception in thread "main" java.lang.NullPointerException 5 调用方法将字符串写入 JAVA 中的 Excel 表时,线程“主”java.lang.NullPointerException 中的异常错误 - Exception in thread “main” java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA 线程“ main”中的异常java.lang.NullPointerException [java] - Exception in thread “main” java.lang.NullPointerException [java] Java:线程“main”java.lang.NullPointerException中的异常 - Java: Exception in thread “main” java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM