Can anyone tells me why there is a NullPointerException in the following code? I tried to figure it out but couldn't!
The exception is in the following line :
companies[x].compName=temp1[0];
The companies array type is Company that contains a String and an Array List.
JFileChooser fileChooser = new JFileChooser(); // create instence from file chooser
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); //assign directory
if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
File f = fileChooser.getSelectedFile();
try{
LineNumberReader lnr = new LineNumberReader(new FileReader(f));
lnr.skip(Long.MAX_VALUE);
int n = lnr.getLineNumber() +1;
lnr.close();
companies = new Company[n];
int i=0 , x=0;
String s ="";
Scanner input = new Scanner(f);
String [] temp1,temp2;
while(input.hasNext()){ // read line by line
s = input.nextLine();
s=s.replaceAll(" ", "");
if(s == null)
continue;
else{
temp1 =s.split(",");
companies[x].compName=temp1[0]; //store compName in the companies
for( i=1;i<temp1.length;i++){
temp2=temp1[i].split("/");
companies[n].compItems.addLast(temp2[0], Integer.parseInt(temp2[1]));
} //end for
} //end else
x++;
} //end while