Results 1 to 5 of 5
  1. #1
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

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

    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.



    Nullpointer exception-nullpointer.png

    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;
    
    }

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

    Default Re: Nullpointer exception

    Which line is 116? is it this one?
    Java Code:
        void testPrint(){
        System.out.println(floorArrayObject[0].bedsitArrayObject[2].stud.studentName);  // here?
        }
    if so, change it to:

    Java 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);  
    }
    and let me know which line is null

  3. #3
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Nullpointer exception

    Here is the error message after running your code:
    Nullpointer exception-test.png

    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:
    Nullpointer exception-error.png
    Last edited by Games2Design; 10-20-2012 at 12:02 AM.

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

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

  5. #5
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default 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:
    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();
    	}
        }
    }
    And the code for when it should create a object of FloorO:
    Java Code:
    		    floorArrayObject[floorOnFil-1]=new FloorO(floorOnFil);
    floorOnFil is 1 in beginning.

Similar Threads

  1. Nullpointer exception
    By naveen516 in forum New To Java
    Replies: 2
    Last Post: 12-19-2011, 05:36 AM
  2. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-29-2010, 12:19 AM
  3. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 05:44 AM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 02:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 04:40 PM

Posting Permissions

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