Results 1 to 4 of 4
Thread: help with these errors
- 04-12-2008, 12:22 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 5
- Rep Power
- 0
help with these errors
i have this error: i will highlight all lines that gave me errors:
Exception in thread "main" java.lang.NullPointerException
at cow.bedCow(cow.java:211)
at seu03Methods.farm(seu03Methods.java:110)
at seu03.main(seu03.java:81)
----------------------------------------------------------------------
ihere is my bedCow method: its goal is to write the array into farmcows.txt
public static void bedCow(cow[]cowList, String barn, int numcows)
{
PrintWriter ofile1 = null;
try
{
ofile1 = new PrintWriter(new File("farmcows.txt"));
ofile1 = new PrintWriter("farmcows.txt");
}
catch(FileNotFoundException e)
{
System.out.println("ERROR");
}
for (int index = 0; index < cowList.length; index++ )
{
ofile1.println(cowList[index].getName()+ "" +"getName" + cowList[index].getBreed()+ "" + "getBreed" + cowList[index].getWeight() +"" + "getWeight" + cowList[index].getGender() + "" + "getGender");
}
ofile1.println();
ofile1.close();
}
--------------------------------------------------------------------------
my seu03Methods:
import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import java.lang.*;
public class seu03Methods
{
//pre: choice
//post: menu option
public static int seu03Menu()
{
String username = System.getProperty ("user.name");
String ESC = "\033[";
System.out.print(ESC + "2J");
System.out.flush();
System.out.println("Hello");
System.out.println("****************************** **********");
System.out.println("Welcome to the Seu03 Menu System, " + username + "!");
System.out.println("Please choose an option : ");
System.out.println("1. Calculator module");
System.out.println("2. Convert dollar to cents");
System.out.println("3. Find the square root of a number");
System.out.println("4. Print a personalized message");
System.out.println("5. Sentence Analysis");
System.out.println("6. Big Money!");
System.out.println("7. Farm Menu");
System.out.println("9. Exit =*(");
int choice = MyMethods.getInt("Please enter a choice: ", 5);
return choice;
}
//pre:
//post:
public static void farmMenu()
{
System.out.println("Welcome to the 1123 Farm");
System.out.println("1: List cows on the farm");
System.out.println("2: Buy a cow (add a cow to the herd)");
System.out.println("3. Send a cow to slaughter house (delete a cow)");
System.out.println("9: Return to seu03");
return;
}
//pre:
//post:
public static void farm ()
{
Scanner cscan = new Scanner(System.in);
boolean runAgain = false;
int check = 0;
int choice = 0;
cow[]cowList = new cow[50];
int cowcount = 0;
cow.getCow(cowList, "farmcows.txt");
do
{
farmMenu();
choice = MyMethods.getInt("Please choose option: ", 5);
switch (choice)
{
case 1:
{
cow.cowReport(cowList, cowcount);
break;
}
case 2:
{
cow.addCow(cowList, cowcount);
break;
}
case 3:
{
cow.deleteCow(cowList, cowcount);
break;
}
case 9:
{
cow.bedCow(cowList,"farmcows.txt", cowcount);
runAgain = true;
break;
}
default:
System.out.println ("Invalid choice. Please try again.");
break;
}//end of switch statement
if (choice == 9)
return;
if(choice != 9)
{
return;
}
}
while(check == 1);
return;
}
-------------------------------------------------------------------------------------------
import java.util.*;
import java.math.*;
import java.io.*;
import java.awt.*;
import java.lang.*;
public class seu03
{
public static void main (String[] args)
{
Scanner cscan = new Scanner(System.in);
boolean runAgain = false;
int check = 0;
int choice = 0;
do
{
choice = seu03Methods.seu03Menu();
switch (choice)
{
case 1:
{
seu03Methods.calculator();
break;
}
case 2:
{
seu03Methods.coins();
break;
}
case 3:
{
seu03Methods.sqrtD();
break;
}
case 4:
{
seu03Methods.message();
break;
}
case 5:
{
seu03Methods.sentenceCheck();
break;
}
case 6:
{
seu03Methods.bigMoney();
break;
}
case 7:
{
seu03Methods.farm();
break;
}
case 9:
{
seu03Methods.exit();
runAgain = false;
break;
}
default:
System.out.println ("Invalid choice. Please try again.");
break;
}//end of switch statement
if (choice == 9)
return;
if(choice != 9)
{
System.out.println("****************************** ****************");
System.out.println ("Would you like to run program again?");
System.out.println ("Yes = 1");
System.out.println ("No = 2");
check = cscan.nextInt ();
if(check == 1)
{
runAgain = true;
}
else
{
runAgain = false;
}
}
}
while(check == 1);
return;
--------------------------------------------------------------------
i also need to return to seu03 if option 9 on the farmMenu is chosen but whenever I chooose it I always get that error.
- 04-12-2008, 02:41 AM #2
Member
- Join Date
- Jan 2008
- Posts
- 21
- Rep Power
- 0
Please fill your code in code bracket.
You can get your answer hereLast edited by banie; 04-12-2008 at 02:45 AM.
- 04-16-2008, 04:39 PM #3
String value stackTrace generated by JVM through console/input stream is understandable and readable enough by a programmer..
There is also a line number that indicates where the exception occured.
banie's reply will help you out....
regards,
sukatoa
- 04-16-2008, 04:55 PM #4
Your line is referencing a place outside of the bounds of the cow list. I cannot give you anything anymore exact. If you look at the place where the length of your list is calculated(which you did not post) i would think that the error is in there. Try outputing the list to the screen and output what your program thinks the length of the list is at the time. And remember that your length will be one number greater than your last array element. If you array goes 0 to 7 then your length will be 8. And if you reference element 8 then there will be a null pointer exception. However i don't think this is part of your error since your for loop goes to less than your length which brings me back to an incorrect length.
Side thought...is Sukatoa a reference to the trigonometry memory tool for sin cos and tangent (soh cah toa)? If not then that is a crazy coincidence.Last edited by Chris.Brown.SPE; 04-16-2008 at 04:58 PM.
Similar Threads
-
Errors I don't understand
By MattyB in forum New To JavaReplies: 4Last Post: 04-01-2008, 11:55 PM -
Handling SQL Errors and Warnings
By Java Tip in forum Java TipReplies: 0Last Post: 02-12-2008, 09:37 AM -
I have 3 errors after compiling
By coco in forum JDBCReplies: 2Last Post: 10-18-2007, 09:32 AM -
Trying to catch thread errors
By yelllow4u in forum New To JavaReplies: 2Last Post: 08-07-2007, 02:52 PM -
Errors in constructor
By ai_2007 in forum Advanced JavaReplies: 0Last Post: 07-01-2007, 05:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks