Results 1 to 10 of 10
  1. #1
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default how to call method?

    I'm getting error I don't know how to call this method here in test file
    error: Exception in thread "main" java.lang.NullPointerException
    at BookTest.printReport(BookTest.java.39)
    at BookTest.main(BookTest.java:17)

    my code
    Java Code:
    public class BookTest
    {
    
            static Book bookArray[] = new Book[4]; 
    	static String dataArray[][] = new String[4][8];
    		
    	public static void main(String args[])
    
    	{
                  
    		buildInstances();
    		printReport();                
                  
    	}
    
    	public static void buildInstances()
    	{
    
    	 	 String dataArray [][] = {{"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"},
    					 {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"},
    					 {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, 
    					 {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}};
    	 
    	}//build instances
    
    
    	public static void printReport()
    	{ //print report
    
                   for (int i=0; i<bookArray.length; i++)
                    {
    
                    	
     			if (dataArray[i][6].equals("Fiction"))
    
                       	    bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(Integer.parseInt(dataArray[i][7]), dataArray[i][8]));
    		
    	
                             
    			if (dataArray[i][6].equals("NonFiction"))
                                bookArray[i]= new NonFiction(dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),"History");
    		
    
    	
                                                           
    	           		
    		}// end of FOR Loop
    
    
                
             	
                    for (int j=0; j<4;j++)
                        {
                         bookArray[j].calculateTotalCharge(Double.parseDouble(dataArray[j][5])+2*3);                
                         System.out.println();
    		     System.out.println();
    
    		    
    			System.out.printf(bookArray[j].toString());
    			
                        } //end of for with j  
    
     		}///print report 	               
    
    
    
    
    
       //  } //end of main
    
    
    } //end class BookTest

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

    Default

    Which line above throws the error? There's an object on that line that has not been initialized, that is null, and you are trying to use it.

    Incidentally, I know which line and object is causing the problem, but if you answer my questions, myself or one the others here will try to help you learn how to solve these errors on your own.

    Best of luck.

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

    Default

    Also, to help you figure this out more, please look at this simple program:

    Java Code:
    public class MyClass
    {
      static int value = 0;
      
      public static void setValueTo3()
      {
        int value = 3;
      }
      
      public static void main(String[] args)
      {
        setValueTo3();
        System.out.println(value);
      }
    }
    Run this program if you can. What will the println in the main method show, 3 or 0?

    So why doesn't it show 3 even though the program sets value to 3 in the setValueTo3() method? The reason is that the value variable in this method is not the same as the one declared in the class. It is a completely separate and independent variable since it has been re-declared.

    This re-declares the variable:
    Java Code:
      public static void setValueTo3()
      {
        int value = 3; // this is a re-declared value variable
      }
    If I want to set the class's value variable, I use the variable in the method above, but I don't declare it. This is the right way to code the method:
    Java Code:
      public static void setValueTo3()
      {
        // there is no "int" in front of the variable here so it
        // is not redeclared
        value = 3; // this changes the class's value variable
      }
    You do something similar in your program. Do you see where?

  4. #4
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    ok I got your point but still i'm getting error
    Java Code:
    public class examp
    {
    
    		static String bookArray[] = new String[5];
    		static String dataArray[][];
    
    	public static void main(String args[])
    	{
    		buildInstances();
    		
    		 printReport();
    		
    
    
    	 }//main
    
    		public static void buildInstances()
    		{	
    
    		   dataArray[][] ={{"HELLO"," MY NAME"," IS"," Alex"," king","jadu"},
    				  {"HELLO"," THIS IS"," SE"," LIN"," SECOND","jadu"},
    				  {"HELLO"," DO U ST"," LL"," REM"," EMBER ","jadu"},
    				  {"HELLO"," 4TH LIN"," IN"," PRO"," GRESS ","jadu"},
    				  {"HELLO"," DOWNTOW"," N "," HUH"," HELLO ","jadu"}};
    
    		}
    					
    
    		public static void printReport()
    		{
    		
    		for (int i=0; i<bookArray.length; i++)
    		{
    		   System.out.println(dataArray[i][0]+dataArray[i][1]+dataArray[i][2]+dataArray[i][3]+dataArray[i][4]+dataArray[i][5]);
    
    	
    		}
    
    		}//print report
    }

  5. #5
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    That is my fault... You need to declair dataArray[][] before you fill it with your values... (I advised it in leapinlizard's other post)
    Who Cares... As Long As It Works...

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

    Default

    Not the same error though. You're finding one of Java's quirks. You can initialize an array as you're doing it, only at declaration.

    In other words, if you declare the array somewhere:
    Java Code:
    // array's declared here
    static String dataArray[][];
    then you can't initialize it somewhere else like this:
    Java Code:
           dataArray[][] ={{"HELLO"," MY NAME"," IS"," Alex"," king","jadu"},
              {"HELLO"," THIS IS"," SE"," LIN"," SECOND","jadu"},
              {"HELLO"," DO U ST"," LL"," REM"," EMBER ","jadu"},
              {"HELLO"," 4TH LIN"," IN"," PRO"," GRESS ","jadu"},
              {"HELLO"," DOWNTOW"," N "," HUH"," HELLO ","jadu"}};

  7. #7
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default

    for e.g.?

    [code]

    static String dataArray[][] = new String [5][5]; ????

  8. #8
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    well I'm off the hook...
    Who Cares... As Long As It Works...

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

    Default

    This could work,
    Java Code:
      public static void buildInstances()
      {
    
        String[][] fooArray =
        {
          {"HELLO", " MY NAME", " IS", " Alex", " king", "jadu"},
          {"HELLO", " THIS IS", " SE", " LIN", " SECOND", "jadu"},
          {"HELLO", " DO U ST", " LL", " REM", " EMBER ", "jadu"},
          {"HELLO", " 4TH LIN", " IN", " PRO", " GRESS ", "jadu"},
          {"HELLO", " DOWNTOW", " N ", " HUH", " HELLO ", "jadu"}
        };
    
        // this will work if you're not using jagged arrays
        dataArray = new String[fooArray.length][fooArray[0].length];
    
        for (int i = 0; i < fooArray.length; i++)
        {
          for (int j = 0; j < fooArray[i].length; j++)
          {
            dataArray[i][j] = fooArray[i][j];
          }
        }
      }
    but only if the arrays are not jagged -- that is all of the "inner" arrays have the same length, and that's not the case in your original program. For that you'll have to initialize your array like so:

    Java Code:
        dataArray = new String[fooArray.length][];
    
        for (int i = 0; i < fooArray.length; i++)
        {
          dataArray[i] = new String[fooArray[i].length];
          for (int j = 0; j < fooArray[i].length; j++)
          {
            dataArray[i][j] = fooArray[i][j];
          }
        }

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

    Default

    Quote Originally Posted by markw8500 View Post
    well I'm off the hook...
    LOL!

    No, I think it's going to take a combined effort here. Seriously, to the original poster, please speak to your instructor without delay and set up some tutoring. You've got some catching up to do it seems, and face-to-fact tutoring will offer the best chances of successfully doing this.

Similar Threads

  1. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM
  2. Call java method from jsp
    By saiphani723 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2009, 11:21 AM
  3. How to call method in servet by using JSP?
    By frankjava1 in forum Java Servlet
    Replies: 2
    Last Post: 10-24-2008, 04:20 AM
  4. Call Java Method
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-15-2008, 07:22 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •