View Single Post
  #1 (permalink)  
Old 04-12-2008, 02:22 AM
oceansdepth oceansdepth is offline
Member
 
Join Date: Mar 2008
Posts: 5
oceansdepth is on a distinguished road
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.
Reply With Quote
Sponsored Links