-1

So, I'm trying to load some values from a JSON file:

{
Float :
null
,
test :
"hello"
,
Int :
2

}

However, the method I have below isn't working at all and doesn't load the file contents. No errors are thrown other than NPEs when I try to use various methods that return values loaded from the file. Any idea what I'm doing wrong?

protected void load(String name, String path) throws FileAlreadyLoadedException{
    if (get(name) == null){
        final ThunderFile tf = new ThunderFile(name, path);
        //files.add(tf);
        try {
            final JSONObject jobj = (JSONObject)new JSONParser().parse(new FileReader(path + File.separator + name + ".json"));
            new Thread(){
                public void run(){
                    Iterator<?> i = jobj.keySet().iterator();
                    while(i.hasNext()){
                        String key = (String) i.next();
                        Object value = jobj.get(key);
                        if (!key.equals("") && !value.equals("")){
                            tf.set(key, value);
                        }
                    }
                    files.add(tf);
                    System.out.println("[Thunderbolt 2] Loaded " + tf.getName() + ".json");
                    this.interrupt();       
                }
            }.start();  
        } catch(IOException | ParseException e) {
            e.printStackTrace();
        }   
    }else{
        throw new FileAlreadyLoadedException("The File " + name + ".json is already loaded!");
    }
}

EDIT: To clarify, no values are loaded, even if my file looked like this:

{"Double":2.0}
  • Well, where are the NPEs thrown exactly? Do you have a stack trace? Maybe it's because your JSON isn't valid? – Todd Jan 09 '15 at 18:59
  • 1
    that is not valid json. – njzk2 Jan 09 '15 at 19:01
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jan 09 '15 at 19:02

1 Answers1

0

Your JSON contains Float: null so Object value = jobj.get(key); for key Float is null.

Therefore !value.equals("") throws NPE.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • It does the same thing with the other value, not just that one. – The Gaming Grunts Jan 09 '15 at 19:03
  • A stacktrace will be more clear than saying things like "the other value". –  Jan 09 '15 at 19:44
  • @jdv Exception in thread "main" [Thunderbolt 2] Loaded test.json java.lang.NullPointerException at me.projectx.thunderbolt2.test.Test.main(Test.java:21) And this is the class: https://gist.github.com/TheGamingGrunts/bc278a5f126680fd4cac – The Gaming Grunts Jan 09 '15 at 19:47
  • @JeanLogeart check my previous comment ;) – The Gaming Grunts Jan 09 '15 at 19:49
  • Well, whatever Thunderbolt.get() is doing, in this case it is returning null. Check the code or API for this method and work from the known to the unknown. –  Jan 09 '15 at 19:55
  • @jdv This is the get method: https://gist.github.com/TheGamingGrunts/16f37c870535095af4ed – The Gaming Grunts Jan 09 '15 at 20:00
  • Ok, so it gets a list of things and iterates over them, looking for a match to the passed-in String. If it finds a match, it returns a thing. If not, it returns null. Whatever is in that ArrayList, it is not a thing that has a String property fetched via getName() that matches "test". You need to look at how this ArrayList is populated, and its contents at runtime. Perhaps some println debugging. NPEs are a runtime issue. –  Jan 09 '15 at 20:25
  • @jdv It's populated via files.add(tf) in the above code. And getName() does return "test". – The Gaming Grunts Jan 09 '15 at 23:23
  • @jdv I seemed to have fixed the issue by readding files.add(tf) under where I created the object. I find it strange how that worked when it wasn't working earlier on. Anyways, thanks for your help :) – The Gaming Grunts Jan 09 '15 at 23:29