Results 1 to 4 of 4
  1. #1
    oceansdepth is offline Member
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

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

  2. #2
    banie is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default

    Please fill your code in code bracket.

    You can get your answer here
    Last edited by banie; 04-12-2008 at 02:45 AM.

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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

  4. #4
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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

  1. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-01-2008, 11:55 PM
  2. Handling SQL Errors and Warnings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-12-2008, 09:37 AM
  3. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 09:32 AM
  4. Trying to catch thread errors
    By yelllow4u in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 02:52 PM
  5. Errors in constructor
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 07-01-2007, 05:35 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
  •