Results 1 to 6 of 6
  1. #1
    k4ff1n34dd1c7 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default Putting a string into ArrayList<String>

    I am trying to read a file and pick out certain pieces of information. When a piece of information come out i need to add it into an arraylist.

    Java Code:
    public void readImports(Scanner sc){
            while(sc.hasNextLine()){
          
                String[] importLineSplit = sc.nextLine().split("\\s+");
                
                for(int i = 1; i < importLineSplit.length; i ++){
                        if(importLineSplit[i].contains(";")) break;
                                if(importLineSplit[i].contains(",")){
                                    importLineSplit[i] = importLineSplit[i].substring(0,(importLineSplit[i].length()-1));                                                      
                                }
                                System.out.println(importLineSplit[i]);
                                MibImport.addMibImport(importLineSplit[i]);
                                
                    }
                }
            }
    This is the method that reads after the word "IMPORT" is found until it reaches a semicolon. Everything works perfectly fine until i reach the line MibImport.addMibImport(importLineSplit[i]); then i get a null pointer exception. MibImport is its own class for all my import data and contains the method
    Java Code:
     public void addMibImport(Object data){
            importsList.add(data);
        }
    this is so i can add each string individually at the end of the arraylist. So how can i add each element to the arraylist without a nullpointer?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you get the exception each time or at the last insertion?

    Since both methods are in the same class what's the point of using the class reference to access a method.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Something's weird here:

    Java Code:
    MibImport.addMibImport(importLineSplit[i]);
    This line calls a class (static) method, but the following declares an instance (nonstatic) method.

    Java Code:
    public void addMibImport(Object data){
    Actual code would help and the actual and complete compiler message(s) that the code gives rise to.

  4. #4
    k4ff1n34dd1c7 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    The compiler gives me null pointer exception then the stack starting at my import line. Its actually at the first insertion. Thats whats throwing me off. I run through the debugger and none of the insertions are null! They are all strings. I am aware that the class is static. I want to put the imports in a different class because i need to add more methods to manipulate the array list. basically the imports text is an object.
    Java Code:
    MibImport.addMibImport(importLineSplit[i]);
    Is my error because when a constant or literal string in that method i get the same error. When i get in work tomorrow i will post the complete code listings.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    importsList is static or not
    importsList assigned to an ArrayList or not?

  6. #6
    k4ff1n34dd1c7 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    importsList is not static and it is assigned to an arrayList.
    But i realized my error i over simplfyed my approach. So i am going to create an ArrayList in my reading class and just extract the data from there. Thanks for all the help. I was trying to create a class using a static methods. rookie mistake.

Similar Threads

  1. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 02:05 PM
  2. how to store an ArrayList<string> into cookies?
    By perplexingtrax in forum Java Servlet
    Replies: 7
    Last Post: 01-12-2009, 01:30 AM
  3. Replies: 5
    Last Post: 01-11-2009, 11:12 AM
  4. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

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