У меня есть приложение со сценарием чтения-записи, которое будет читать и записывать набор ссылок и флажков, а также читать или записывать файл JSON
. Все части приложения работают нормально, кроме случаев, когда я пытаюсь прочитать файл JSON
. Я пробовал использовать как FileReader
, так и InputStream
, но не могу заставить их работать в любом случае.
FileWriter
отлично работает в Eclipse IDE, ( Последняя версия, работает JDK 11
, то же самое, что и версия java в моем P C), но не тогда, когда я упаковываю его в работоспособную банку. Я также пробовал InputStream
, но это не работает ни в среде IDE, ни в упакованном файле jar.
Я использую Google JSON simple, если это тоже помогает. Я также загрузил экспорт в файловую систему всего проекта, если это необходимо GDriveLink (это проект для начинающих для школы, поэтому он не выглядит слишком красиво внутри.)
Код для FileWriter
:
public void ReadToJson() { //I've tried this as static and non static
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("Links.json")) //line 93
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray userList = (JSONArray) obj;
System.out.println(userList);
//Iterate over user array
userList.forEach( emp -> parseUserObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseUserObject(JSONObject user)
{
//Get user object within list
JSONObject userObject = (JSONObject) user.get("linkSet1");
//Get 1st link data
link1 = (String) userObject.get("jlink1");
link1Cb = (String) userObject.get("jlink1Cb");
System.out.println(link1);
System.out.println(link1Cb);
//Get 2nd link data
link2 = (String) userObject.get("jlink2");
link2Cb = (String) userObject.get("jlink2Cb");
System.out.println(link2);
System.out.println(link2Cb);
//Get 3rd link data
link3 = (String) userObject.get("jlink3");
link3Cb = (String) userObject.get("jlink3Cb");
System.out.println(link3);
System.out.println(link3Cb);
//Get 4th link data
link4 = (String) userObject.get("jlink4");
link4Cb = (String) userObject.get("jlink4Cb");
System.out.println(link4);
System.out.println(link4Cb);
//Get 5th link data
link5 = (String) userObject.get("jlink5");
link5Cb = (String) userObject.get("jlink5Cb");
System.out.println(link5);
System.out.println(link5Cb);
//Get first link data
link6 = (String) userObject.get("jlink6");
link6Cb = (String) userObject.get("jlink6Cb");
System.out.println(link6);
System.out.println(link6Cb);
}
}
Журналы консоли для FileWriter
после вызова функции ReadToJson()
:
java.io.FileNotFoundException: Links.json (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at mainFolder.common.RWJsonBTabs.ReadToJson(RWJsonBTabs.java:93)
at mainFolder.views.panelBrowser$3.actionPerformed(panelBrowser.java:159)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6631)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6396)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5007)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Код для InputStream
:
public void ReadToJson() {
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (InputStream inputStream = getClass().getResourceAsStream("Links.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) //Line 94
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray userList = (JSONArray) obj;
System.out.println(userList);
//Iterate over user array
userList.forEach( emp -> parseUserObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseUserObject(JSONObject user)
{
//Get user object within list
JSONObject userObject = (JSONObject) user.get("linkSet1");
//Get 1st link data
link1 = (String) userObject.get("jlink1");
link1Cb = (String) userObject.get("jlink1Cb");
System.out.println(link1);
System.out.println(link1Cb);
//Get 2nd link data
link2 = (String) userObject.get("jlink2");
link2Cb = (String) userObject.get("jlink2Cb");
System.out.println(link2);
System.out.println(link2Cb);
//Get 3rd link data
link3 = (String) userObject.get("jlink3");
link3Cb = (String) userObject.get("jlink3Cb");
System.out.println(link3);
System.out.println(link3Cb);
//Get 4th link data
link4 = (String) userObject.get("jlink4");
link4Cb = (String) userObject.get("jlink4Cb");
System.out.println(link4);
System.out.println(link4Cb);
//Get 5th link data
link5 = (String) userObject.get("jlink5");
link5Cb = (String) userObject.get("jlink5Cb");
System.out.println(link5);
System.out.println(link5Cb);
//Get first link data
link6 = (String) userObject.get("jlink6");
link6Cb = (String) userObject.get("jlink6Cb");
System.out.println(link6);
System.out.println(link6Cb);
}
Журналы консоли для InputStream
после вызова функции ReadToJson()
:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.base/java.io.Reader.<init>(Reader.java:167)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at mainFolder.common.RWJsonBTabs.ReadToJson(RWJsonBTabs.java:94)
at mainFolder.views.panelBrowser$3.actionPerformed(panelBrowser.java:159)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6631)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6396)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5007)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)