public class Employees {
private String[] onomata,eponyma;
private int[] kwdikoi;
private int i,kwdikos;
private String onoma,eponymo;
private long kwdikoslong;
{
FileReader reader = null;
try {
reader= new FileReader("employees.json");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
JSONParser jsonparser= new JSONParser();
try {
JSONObject jsonobj = (JSONObject) jsonparser.parse(reader);
JSONArray baseArray =(JSONArray) jsonobj.get("employees");
for (int i=0;i<baseArray.size();i++)
{
JSONObject jsonobj2 = (JSONObject) baseArray.get(i);
onoma=(String) jsonobj2.get("onoma");
onomata[i]=onoma;
eponymo=(String) jsonobj2.get("eponymo");
eponyma[i]=eponymo;
kwdikoslong=(long) jsonobj2.get("kwdikos");
kwdikos=(int)kwdikoslong;
kwdikoi[i]=kwdikos;
}
}
catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
Asked
Active
Viewed 46 times
-1

Harshal Parekh
- 5,918
- 4
- 21
- 43

kyriakos
- 3
- 1
-
2After getting a `FileNotFoundException` do you think it is wise to continue? – Scary Wombat Apr 30 '20 at 00:48
-
1Please show the stacktrace. – Harshal Parekh Apr 30 '20 at 00:50
-
Also, post the employees.json file. – Sara Apr 30 '20 at 00:51
-
here is the employees.json file { "employees": [ {"onoma":"Kyriakos","eponymo":"Theodoridis","kwdikos":3511}, {"onoma":"Dimitris","eponymo":"Soukos","kwdikos":3522}, {"onoma":"Tasos","eponymo":"Gkortsopoulos","kwdikos":3533}, {"onoma":"Aggelos","eponymo":"Gogos","kwdikos":3544}, {"onoma":"Giorgos","eponymo":"Mpoulari","kwdikos":3555}, {"onoma":"Aris","eponymo":"Gouri","kwdikos":3566}, {"onoma":"Giorgos","eponymo":"Georgis","kwdikos":3577}, {"onoma":"Panos","eponymo":"Vadiakas","kwdikos":3588}, {"onoma":"Aggelos","eponymo":"Babis","kwdikos":3599}, ] } – kyriakos Apr 30 '20 at 00:54
-
Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Mad Physicist Apr 30 '20 at 00:55
-
1You need to edit the question and put the new information there. Not in comments. – Harshal Parekh Apr 30 '20 at 00:55
-
I keep getting this : Exception in thread "main" java.lang.NullPointerException at project2.Employees.
(Employees.java:37) – kyriakos Apr 30 '20 at 00:55 -
Which line is #37? – Harshal Parekh Apr 30 '20 at 00:57
-
onomata[i]=onoma; – kyriakos Apr 30 '20 at 00:59
-
`private String[] onomata,eponyma;` declares the variables, but they are still null. Why not use an ArrayList instead? – Scary Wombat Apr 30 '20 at 01:20
1 Answers
0
Your problem is here:
private String[] onomata, eponyma;
The variable onomata
is never initialized.
try {
JSONObject jsonobj = (JSONObject) jsonparser.parse(reader);
JSONArray baseArray =(JSONArray) jsonobj.get("employees");
// initialize it here
onomata = new String[baseArray.size()];
for (int i = 0; i< baseArray.size(); i++)
{
JSONObject jsonobj2 = (JSONObject) baseArray.get(i);
onoma=(String) jsonobj2.get("onoma");
onomata[i]=onoma;
eponymo=(String) jsonobj2.get("eponymo");
eponyma[i]=eponymo;
kwdikoslong=(long) jsonobj2.get("kwdikos");
kwdikos=(int)kwdikoslong;
kwdikoi[i]=kwdikos;
}
}
catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
This will not fix all the exceptions. As and when you encounter them, start initializing the arrays and fix them.
Side notes:
From here, you should not continue your code when a FileNotFoundException
occurs.
It is better to do this:
jsonobj2.getString("onoma", "DEFAULT");
// than this: (String) jsonobj2.get("onoma")
This is the generic solution to all the NullPointerException
s.

Harshal Parekh
- 5,918
- 4
- 21
- 43
-
Thank you, but what about kwdikoi, as you said this will not fix all exceptions. This worked for onomata and eponyma but the error keeps at kwdikoi now. How I can initialize this because it is integer. Sorry if i am asking something obvious but i am new! – kyriakos Apr 30 '20 at 01:37
-
`kwdikoi` is an integer array so it will be `kwdikoi = new int[baseArray.size()];` – Harshal Parekh Apr 30 '20 at 01:38