-1

I have a JSON file which contains a dictionary (an actual English to Persian dictionary) and I wrote a java Class to read it and it doesnt work i wrote a function which i use in to read a particular part of the file.

This is my reader Class :

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JSONReader {
    static JSONParser parser = new JSONParser();
    static JSONArray dictarray;
    public static void main(String[] args) throws FileNotFoundException, ParseException, IOException {
        Object obj = parser.parse(new FileReader("C:\\Users\\Mohsen\\Documents\\NetBeansProjects\\DSFP2\\dictionary.JSON"));
        JSONArray dictarray = (JSONArray)obj;
    }

    public void read() {
        System.out.println(dictarray.get(0));
    }
}

this is the FXMLController file which makes a new object of this class and tries to use it:

public class FXMLDocumentController implements Initializable {
    JSONReader JR = new JSONReader();

    @FXML
    private void action (KeyEvent event) {
        JR.read();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }    

}

And this is the error i get:

Executing C:\Users\Mohsen\Documents\NetBeansProjects\DSFP2\dist\run1082927040\DSFP2.jar using platform C:\Program Files\Java\jdk1.8.0_45\jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3965)
    at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3911)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2502)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:197)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:147)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$349(GlassViewEventHandler.java:228)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$195/1857609670.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:227)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
    at com.sun.glass.ui.View.notifyKey(View.java:956)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
    ... 31 more
Caused by: java.lang.NullPointerException
    at JSONReader.read(JSONReader.java:17)
    at FXMLDocumentController.action(FXMLDocumentController.java:11)
    ... 41 more

Where's the problem?

1 Answers1

0

You are reading the file in the main method of the JSONReader class, which is never executed as this is not the main class of the application.

Put that code into the read method:

public class JSONReader {
    JSONArray dictarray;

    public void read()  throws FileNotFoundException, ParseException, IOException {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("C:\\Users\\Mohsen\\Documents\\NetBeansProjects\\DSFP2\\dictionary.JSON"));
        dictarray = (JSONArray)obj;
    }
}

Edit 01.01.2020:

To only read the file once, move that code into a separate method - I named it init, and call that once, you could also put it in a constructor. Then in the read method you cann access the data you read i previously.

public class JSONReader {
    static JSONArray dictarray;

    public void init()  throws FileNotFoundException, ParseException, IOException {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader("C:\\Users\\Mohsen\\Documents\\NetBeansProjects\\DSFP2\\dictionary.JSON"));
        dictarray = (JSONArray)obj;
    }

    public Object read() {
      return dictarray.get(0);
    }
}
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • But in this solution, every time i want to read from the file i have to open it! I was trying to write in a way that only 1 opening would be enough! But tnQ anyways! – Mohsen Amiri Amjad Dec 31 '19 at 12:58
  • @MohsenAmiriAmjad Why do you need to do you need multiple instances of the `JSONReader` class? If your program is properly designed, you should be able to share the data between the parts of the code that require access. If you insist on using `static` data (which is bad practice), you could still move the file reading to a `static` initializer block or use a singleton. Note that in real code hardcoding a file path like this is a no-go... – fabian Dec 31 '19 at 20:47
  • @MohsenAmiriAmjad I updated my answer, And i removed the static from the field, thanks to @ fabian, I missed that. – P.J.Meisch Jan 01 '20 at 14:05