Results 1 to 5 of 5
Thread: Nullpointer exception
- 10-19-2012, 10:38 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Nullpointer exception
Ok, my task is to make a Bedsit program as assignment with three floors and in each floor there is 6 bedsits.
Now before program runs your asked to read data from a file (called hybeldata.txt):
Now i know bedsit 1C is empty (tom hybel = empty bedsit). I encountered a problem when i tried to check for any bedsits that have no tenant with a two for loops with a if test in it(in method newTenant()). I got nullpointer exception.Java Code:10;2012;0;0;6000;7000; 1;A;6000;Ole Johan 1;B;12000;Erik Smith 1;C;0;TOM HYBEL 1;D;9000;Alan Turen 1;E;33000;Tim Verner 1;F;4999;Anders Tanen 2;A;9000;Torvald Linus 2;B;5001;Rikard Stall 2;C;10000;Bill Weitz 2;D;15000;Steve Job 2;E;123;Steve Woz 2;F;2400;Denis Rich 3;A;88000;James Goblin 3;B;456000;Mark Sukker 3;C;6000;Karl Bagasje 3;D;0;TOM HYBEL 3;E;250;Ada Love 3;F;55000;Don Knut
So i thought to narrow the problem down by removing the for loops and if test, and try to simply write out bedsit 1C's name out to check if reading data from the file and then onto Bedsit attributes (hybeldata.txt) has been successful.
As you can see in "testPrint()" i tried that. But i still receive nullpointer exception.
Now to my knowledge the code looks right. So i don't get why i receive that.

Java Code:willinglyimport java.util.Scanner; import java.io.*; class Oblig3{ public static void main(String[]args){ Bedsits arrow=new Bedsits(); arrow.mainMetod(); } } class Bedsits{ Scanner in=new Scanner(System.in); FloorO[]floorArrayObject=new FloorO[3]; void mainMetod(){ Scanner readFil; try{ readFil=new Scanner(new File("hybeldata.txt")); String line=readFil.nextLine(); String[]splitLine=line.split(";"); int month=Integer.parseInt(splitLine[0]); int year=Integer.parseInt(splitLine[1]); int profit=Integer.parseInt(splitLine[2]); int amountMonth=Integer.parseInt(splitLine[3]); int lowerRent=Integer.parseInt(splitLine[4]); int topRent=Integer.parseInt(splitLine[5]); int i=0; int j=0; while(!readFil.hasNextLine()){ String line2=readFil.nextLine(); String[]splitUpLine=line2.split(";"); int floorOnFil=Integer.parseInt(splitUpLine[0]); char letter=line2.charAt(1); int balance=Integer.parseInt(splitUpLine[2]); String name=splitUpLine[3]; floorArrayObject[i].floor=floorOnFil; floorArrayObject[i].bedsitArrayObject[j].stud.studentName=name; floorArrayObject[i].bedsitArrayObject[j].stud.studBalance=balance; floorArrayObject[i].bedsitArrayObject[j].bedsitLetter=letter; if(j==5){ i++; } } readFil.close(); }catch(Exception e){ e.printStackTrace(); } int command=10; do { testPrint(); System.out.println("1. Write overview"); System.out.println("2. Registrate a new tenant"); System.out.println("3. Registrate payment from tenant"); System.out.println("4. Regristrate tenant moving out"); System.out.println("5. Monthly run of tenants"); System.out.println("6. Throw out tenant"); System.out.println("7. Increase rent"); System.out.println("8. End"); System.out.println("\nChoose command:"); command=in.nextInt(); switch(command){ case 2:newTenant();break; } } while(command!=8); System.out.println("Program ended."); } int charToInt(char letter) { int index=0; char [] bedsitCharArray={'A','B','C','D','E','F'}; for(int i=0;i<bedsitCharArray.length;i++){ if(bedsitCharArray[i]==letter){ index=i; } } return index; } void newTenant(){ for(int i=0;i<floorArrayObject.length;i++){ } } void testPrint(){ System.out.println(floorArrayObject[0].bedsitArrayObject[2].stud.studentName); } } class FloorO{ final int rentLow=6000; final int rentTop=7000; int floor; Bedsit[]bedsitArrayObject=new Bedsit[6]; FloorO(int floorIn){ floor=++floorIn; for(int i=0;i<bedsitArrayObject.length;i++){ bedsitArrayObject[i]=new Bedsit(); } } } class Bedsit{ char bedsitLetter; Student stud=new Student(); } class Student{ String studentName; int studBalance; }
-
Re: Nullpointer exception
Which line is 116? is it this one?
if so, change it to:Java Code:void testPrint(){ System.out.println(floorArrayObject[0].bedsitArrayObject[2].stud.studentName); // here? }
and let me know which line is nullJava Code:void testPrint(){ System.out.println("(floorArrayObject == null): " + (floorArrayObject == null)); System.out.println("(floorArrayObject[0] == null): " + (floorArrayObject[0] == null)); System.out.println("(floorArrayObject[0].bedsitArrayObject == null): " + (floorArrayObject[0].bedsitArrayObject == null)); System.out.println("(floorArrayObject[0].bedsitArrayObject[2] == null): " + (floorArrayObject[0].bedsitArrayObject[2] == null)); System.out.println("(floorArrayObject[0].bedsitArrayObject[2].stud == null): " + (floorArrayObject[0].bedsitArrayObject[2].stud == null)); System.out.println("(floorArrayObject[0].bedsitArrayObject[2].stud.studentName == null): " + (floorArrayObject[0].bedsitArrayObject[2].stud.studentName == null)); System.out.println(floorArrayObject[0].bedsitArrayObject[2].stud.studentName); }
- 10-19-2012, 10:55 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: Nullpointer exception
Here is the error message after running your code:

I will try to see if i can interpret your code (as i get your general idea with it).
EDIT: I believe floorArrayObject[0] and the rest are NULL instead of containing a object of Bedsit class.
EDIT2:
After interpretating your test, i realized i had not made objects of FloorO class. But i then included that in the code, but then receive a error:
Last edited by Games2Design; 10-19-2012 at 11:02 PM.
-
Re: Nullpointer exception
The error seems to be telling you exactly what's wrong. Does Floor0 have a default constructor (a constructor with no parameters)? If not, then use the constructors that it already has, or give it a default constructor -- but only if it's appropriate to do so.
- 10-20-2012, 12:15 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: Nullpointer exception
FloorO has a constructor that i use to set it's floor variable to the in-parameter (which is sent when a FloorO object is created).
I also used the constructor to create Bedsit objects.
Sigh why is it not working :S It's telling me that floorArrayObject[0] isn't pointing at an object of FloorO, even though it is:
Here is the class for FloorO:
And the code for when it should create a object of FloorO:Java Code:class FloorO{ final int rentLow=6000; final int rentTop=7000; int floor; Bedsit[]bedsitArrayObject=new Bedsit[6]; FloorO(int floorOnFile){ floor=floorOnFile; for(int i=0;i<bedsitArrayObject.length;i++){ bedsitArrayObject[i]=new Bedsit(); } } }
floorOnFil is 1 in beginning.Java Code:floorArrayObject[floorOnFil-1]=new FloorO(floorOnFil);
Similar Threads
-
Nullpointer exception
By naveen516 in forum New To JavaReplies: 2Last Post: 12-19-2011, 04:36 AM -
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks