Results 1 to 6 of 6
- 03-20-2009, 05:52 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 7
- Rep Power
- 0
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.
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 methodJava 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 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?Java Code:public void addMibImport(Object data){ importsList.add(data); }
- 03-21-2009, 03:49 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-21-2009, 04:44 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Something's weird here:
This line calls a class (static) method, but the following declares an instance (nonstatic) method.Java Code:MibImport.addMibImport(importLineSplit[i]);
Actual code would help and the actual and complete compiler message(s) that the code gives rise to.Java Code:public void addMibImport(Object data){
- 03-22-2009, 09:07 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 7
- Rep Power
- 0
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.
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.Java Code:MibImport.addMibImport(importLineSplit[i]);
- 03-23-2009, 03:52 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
importsList is static or not
importsList assigned to an ArrayList or not?
- 03-23-2009, 05:10 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 7
- Rep Power
- 0
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
-
combine string[] into string like perl's join function
By tekberg in forum Advanced JavaReplies: 9Last Post: 02-23-2009, 01:05 PM -
how to store an ArrayList<string> into cookies?
By perplexingtrax in forum Java ServletReplies: 7Last Post: 01-12-2009, 12:30 AM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks