Problems with loading a comma-seperated value file.
I'm getting the error:
SEVERE: null
java.lang.NullPointerException
----
It's loading a file with the values:
Code:
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,100,0,0
0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0
5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0
The file itself is kind of messy, but I have it here. It's save and load in one file - so you can also see the saving process as well to help me determine what I'm doing wrong.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package valerian;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Jewels
*/
public class CSVLoadSave
{
public void loadRead(String characterName) throws Exception {
try (BufferedReader CSVFile = new BufferedReader(new FileReader( "C:\\vdssaves\\" + characterName + ".vds" ))) { // vds = valerian data sheet
String dataRow = CSVFile.readLine();//1 line
while (dataRow != null) {
String[] dataArray = dataRow.split(","); // SPLITS DATA BY COMMAS - Typically, a normal csv file, but I used vds
dataRow = CSVFile.readLine(); /// reads next line 2 line
String[] dataArray2 = dataRow.split(","); //2
dataRow = CSVFile.readLine();// 3
String[] dataArray3 = dataRow.split(","); // 3
dataRow = CSVFile.readLine(); // 4 line
String[] playerEquipmentS = dataRow.split(","); //4
dataRow = CSVFile.readLine();
String[] invi = dataRow.split(","); // 5 line
dataRow = CSVFile.readLine(); // 5
String[] invc = dataRow.split(","); // 6 line
dataRow = CSVFile.readLine();
String[] currency = dataRow.split(","); // 7 line
int da1[] = new int[50];
int da2[] = new int[5];
int da3[] = new int[5];
for (int i = 0; i < 50; i++)
da1[i] = Integer.parseInt(dataArray[i]);
for (int i = 0; i < 5; i++)
da2[i] = Integer.parseInt(dataArray2[i]);
for (int i = 0; i < 5; i++)
da3[i] = Integer.parseInt(dataArray3[i]);
Valerian.player.loadCharacter(da1, da2, da3);
int[] peq = new int[13];
for (int i = 0; i < 13; i++)
peq[i] = Integer.parseInt(playerEquipmentS[i]);
Valerian.playerEq.setPEQ(peq[0],peq[1],peq[2],peq[3],peq[4],peq[5],peq[6],peq[7],peq[8],peq[9],peq[10],peq[11],peq[12]);
int[] pinv = new int[50];//itemIDs
for (int i = 0; i < 50; i++) // good^-----
pinv[i] = Integer.parseInt(invi[i]);
int[] cinv = new int[50]; //counts
for (int i = 0; i < 50; i++)
cinv[i] = Integer.parseInt(invc[i]);
int[] currint = new int[2];
for (int i = 0; i < 2; i++)
currint[i] = Integer.parseInt(currency[i]);
}
}
catch (FileNotFoundException ex) {
System.out.println("Directory does not exist!");
}
}
public void load()
{
System.out.println("What is your character's name?");
Valerian.kb.nextLine();
String characterName = Valerian.kb.nextLine();
try {
loadRead(characterName);
} catch (Exception ex) {
Logger.getLogger(CSVLoadSave.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void save() throws Exception
{
Valerian.kb.nextLine(); // clears buffer
System.out.println("What is your character's name?");
String characterName = Valerian.kb.nextLine();
File x = new File("C:\\vdssaves\\" + characterName + ".vds");
int i = 0; // iterator for if statement
if(!x.exists())
{
try {
saveWrite(characterName);
} catch (Exception ex) {
Logger.getLogger(CSVLoadSave.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (x.exists())
{
char yn = 'f';
System.out.println("Overwrite file?");
while (yn != 'y' && yn != 'n') {
String yesNo = Valerian.kb.nextLine();
yesNo = yesNo.toLowerCase();
yn = yesNo.charAt(0);
}
if (yn == 'y') {
x.delete();
saveWrite(characterName);
}
else if (yn == 'n')
{
System.out.println("File overwrite terminated.");
}
}
}
public void saveWrite(String characterName) throws Exception {
File f = new File("C:\\vdssaves\\"+characterName+".vds"); // for if statement
if (!f.exists()) // if the directory doesn't exist
{
boolean mkdir = new File("C:\\vdssaves").mkdir(); // make this directory so that you don't get FileNotFoundException
}
try (BufferedWriter CSVFile = new BufferedWriter(new FileWriter( "C:\\vdssaves\\" + characterName + ".vds" ))) { // vds = valerian data sheet
int[] skillIDArray = new int[50];
for(int i = 0; i < 50; i++){
skillIDArray[i] = Valerian.player.getSkillID(i); }
for (int i = 0; i < 49; i++)
CSVFile.write( + skillIDArray[i] + ",");
CSVFile.write(skillIDArray[49] + System.getProperty("line.separator"));
System.out.print("Saving"); // save status checkpoint 1
int[] plstat = Valerian.player.playerStatus();
for (int i = 0; i < 4; i++)
CSVFile.write(plstat[i] + ",");
CSVFile.write(plstat[4] + System.getProperty("line.separator"));
System.out.print("."); // save status checkpoint 2
int[] plskill = Valerian.player.playerSkillArray();
for (int i = 0; i < 4; i++)
CSVFile.write(plskill[i] + ",");
CSVFile.write(plskill[4] + System.getProperty("line.separator"));
System.out.print(".."); // save status checkpoint 3
int[] peq = Valerian.playerEq.getPEQ();
for (int i = 0; i < 12; i++)
CSVFile.write(peq[i] + ",");
CSVFile.write(peq[12] + System.getProperty("line.separator"));
System.out.print("."); // cp 4
int[] pinv = Valerian.inventory.getInventory();//itemIDs
for (int i = 0; i < 49; i++)
CSVFile.write(pinv[i] + ",");
CSVFile.write(pinv[49] + System.getProperty("line.separator"));
System.out.print(" "); // cp 5
int[] cinv = Valerian.inventory.getInventoryCount();//counts
for (int i = 0; i < 49; i++)
CSVFile.write(cinv[i] + ",");
CSVFile.write(cinv[49] + System.getProperty("line.separator"));
System.out.print("Sav"); // cp 6
int[] currint = { (Valerian.inventory.getGold()), (Valerian.inventory.getDiamond()) };
CSVFile.write(currint[0]+","+currint[1]);
CSVFile.close();
System.out.print("ed!\n");// final cp 7
}
}
}
Any help is widely appreciated. :) Thanks for reading, even if you can't help me.
Re: Problems with loading a comma-seperated value file.
Post your error stack trace so we know which line in your code that produced the NPE exception.
Re: Problems with loading a comma-seperated value file.
Jun 08, 2012 8:04:09 PM valerian.CSVLoadSave load
SEVERE: null
java.lang.NullPointerException
at valerian.CSVLoadSave.loadRead(CSVLoadSave.java:22)
at valerian.CSVLoadSave.load(CSVLoadSave.java:71)
at valerian.Valerian.load(Valerian.java:152)
at valerian.Valerian.choices(Valerian.java:86)
at valerian.Valerian.main(Valerian.java:50)
Re: Problems with loading a comma-seperated value file.
Quote:
ava.lang.NullPointerException
at valerian.CSVLoadSave.loadRead(CSVLoadSave.java:22)
Look at line 22 and find the variable with the null value. Then backtrack in the code to find out why that variable does not have a value non-null value.
Re: Problems with loading a comma-seperated value file.
The data line that line 22 is reading reads "0,0,100,0,0". I can't find a null value. I looked at the saving process and it writes all five elements. As far as I can tell, there is no null value. But of course the compiler's always right :p
Re: Problems with loading a comma-seperated value file.
No, what line of code is throwing the NPE? Please post that line.
Re: Problems with loading a comma-seperated value file.
dataRow = CSVFile.readLine(); /// reads next line 2 (this is line 21)
String[] dataArray2 = dataRow.split(","); //2 (this is line 22)\
If you need further reference to the file, it's up in the original post. And I thank you all for your help. :)
Re: Problems with loading a comma-seperated value file.
The readLine() method can return a null value that your code needs to test for.
To see if that is what is happening add a call to println after line 21 to print out the value of dataRow.
Re: Problems with loading a comma-seperated value file.
So you're trying to split a null String. Add println's to verify this:
Code:
System.out.println("A) dataRow := " + dataRow);
String[] dataArray = dataRow.split(",");
dataRow = CSVFile.readLine(); /// reads next line 2 line
System.out.println("B) dataRow := " + dataRow);
String[] dataArray2 = dataRow.split(","); //2
dataRow = CSVFile.readLine();// 3
System.out.println("C) dataRow := " + dataRow);
String[] dataArray3 = dataRow.split(","); // 3
etc...
You should probably read in your data with more care, checking after each readLine() that the String returned isn't null. This is often done inside of a loop.
Re: Problems with loading a comma-seperated value file.
0,0,100,0,0
null
That is what it prints out... now the question is why there is a null value after a line break.
Re: Problems with loading a comma-seperated value file.
What happens when you run out of file? If you've read the last line of the file? What in your code checks for this and allows the reading to stop?
Re: Problems with loading a comma-seperated value file.
Code:
try (BufferedReader CSVFile = new BufferedReader(new FileReader( "C:\\vdssaves\\" + characterName + ".vds" ))) { // vds = valerian data sheet
String dataRow = CSVFile.readLine();//1 line
while (dataRow != null) {
I also realized that I forgot to add the CSVFile.close(); at the end of the while loop. Sadly enough, it did not solve my problem :\
Re: Problems with loading a comma-seperated value file.
You need to test for null after EVERY call to readLine() unless you know the contents of the input file has as many groups of lines as there are readLine()s in the loop. If there are 5 readLine() calls in the loop then the file must have a multiple of 5 lines. For example: 5, 10, 15, 20 etc
Re: Problems with loading a comma-seperated value file.
Okay, I get what you're saying. But I have the same number of lines as readLine() calls. So going about testing for null, how would I do that to search for a null value in a String and what do I do if it's found?
Furthermore, should I add a comma after the end number to split the null value? Would that hurt the process? I'm going to try that real quick. Probably won't do anything, but worth a shot.
UD: Extra comma did nothing :p
Re: Problems with loading a comma-seperated value file.
Quote:
search for a null value in a String
That won't happen. A String does not contain values other than characters.
The readLine() method returns either a String or a null value.
That is how you test if dataRow has a null value or not
Re: Problems with loading a comma-seperated value file.
It's not that one particular dataRow is completely null. I printed the String dataRow (the second one dataRow - the one I'm getting the error on) and it's printing
There's the five values that are necessary - which I need. But then, it includes a line break and a null value.
Re: Problems with loading a comma-seperated value file.
Where is the null value coming from? Can you post the around where null is printed to show where the null value is coming from?
Re: Problems with loading a comma-seperated value file.
Lines 127-131 on original post:
Code:
int[] plstat = Valerian.player.playerStatus();
for (int i = 0; i < 4; i++)
CSVFile.write(plstat[i] + ",");
CSVFile.write(plstat[4] + System.getProperty("line.separator"));
System.out.print("."); // save status checkpoint 2
Re: Problems with loading a comma-seperated value file.
I don't see any print statement that would print null? Where is the null being printed?
Where is the null value coming from?
Re: Problems with loading a comma-seperated value file.
That's what is stumping me. I looked back at where the variables are imported, all of the elements are fine. In fact, all of them print correctly. It's just that extra null after the breakline.
If I bring the savefiles (ones created most recently), it shows no "NUL" characters. Only "CR|LF" characters, which are breaks. It shouldn't in any way be reading that null value whatsoever.