Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree3Likes

Thread: Problems with loading a comma-seperated value file.

  1. #1
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Unhappy 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:
    Java 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.

    Java 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.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default 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.
    Tolls likes this.

  3. #3
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Problems with loading a comma-seperated value file.

    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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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
    Last edited by dddJewelsbbb; 06-09-2012 at 02:54 PM. Reason: clarification

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problems with loading a comma-seperated value file.

    No, what line of code is throwing the NPE? Please post that line.

  7. #7
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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. :)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problems with loading a comma-seperated value file.

    So you're trying to split a null String. Add println's to verify this:

    Java 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.

  10. #10
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  12. #12
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default Re: Problems with loading a comma-seperated value file.

    Java 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 :\
    Last edited by dddJewelsbbb; 06-09-2012 at 03:12 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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
    Last edited by dddJewelsbbb; 06-09-2012 at 03:51 PM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Problems with loading a comma-seperated value file.

    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.

    dataRow == null
    That is how you test if dataRow has a null value or not
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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
    Java Code:
    0,0,100,0,0
    null
    There's the five values that are necessary - which I need. But then, it includes a line break and a null value.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default Re: Problems with loading a comma-seperated value file.

    Lines 127-131 on original post:
    Java 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

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    dddJewelsbbb is offline Computer Slave
    Join Date
    Jun 2012
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading a comma separated file into JTextPane
    By arndtmatt in forum New To Java
    Replies: 3
    Last Post: 04-09-2012, 10:08 PM
  2. MySQLDump to comma dilim text file
    By Sobutai in forum New To Java
    Replies: 1
    Last Post: 05-11-2011, 07:39 AM
  3. Java Regular Expressions: Comma Seperated List
    By sgtblitz in forum New To Java
    Replies: 3
    Last Post: 04-18-2011, 09:17 PM
  4. Delimite the file using comma
    By gokulcool in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:40 PM
  5. Replies: 7
    Last Post: 05-23-2008, 03:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •