Results 1 to 10 of 10
Thread: how to call method?
- 04-29-2009, 10:45 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
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
-
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.
-
Also, to help you figure this out more, please look at this simple program:
Run this program if you can. What will the println in the main method show, 3 or 0?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); } }
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:
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() { int value = 3; // this is a re-declared value variable }
You do something similar in your program. Do you see where?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 }
- 04-29-2009, 11:40 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
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 }
- 04-29-2009, 11:44 PM #5
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...
-
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:
then you can't initialize it somewhere else like this:Java Code:// array's declared here static String dataArray[][];
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"}};
- 04-29-2009, 11:50 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
for e.g.?
[code]
static String dataArray[][] = new String [5][5]; ????
- 04-29-2009, 11:53 PM #8
well I'm off the hook...
Who Cares... As Long As It Works...
-
This could work,
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: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]; } } }
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]; } }
-
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
-
How to call a class within a method
By Manfizy in forum New To JavaReplies: 3Last Post: 03-19-2009, 12:34 PM -
Call java method from jsp
By saiphani723 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-25-2009, 11:21 AM -
How to call method in servet by using JSP?
By frankjava1 in forum Java ServletReplies: 2Last Post: 10-24-2008, 04:20 AM -
Call Java Method
By hussainzim in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-15-2008, 07:22 AM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks