Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Need some help getting this code to run...

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help getting this code to run...

    I compiled my code but it will not output. I get an error that points to

    public String toString()
        {
            String s= "ALL PACKETS \n";
            for(Packet mypacket : shipment)
                s += mypacket;
            return s;

    it says java.lang.NullException Null...

    public class Packet
    {
        // instance variables - replace the example below with your own
        private int idNumber;
        private String state;
        public double weight;
     
        public Packet(int id, String s, double d)
        {
            this.idNumber = id;
            this.state = s;
            this.weight = d;
        }
     
        public boolean isHeavy()
        {
            if (weight > 12)
                return true;
            else
                return false;
        }
     
        public boolean isLight()
        {
            if (weight < 7)
                return true;
            else
                return false;
        }
     
        public String toString()
        {
            return "ID: " + idNumber + "\t" + "Weight: " + weight + "\t" + "State: " + state;
        }
    }

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
     
    public class Packages
    {
        double totalWeight;
        public ArrayList<Packet> shipment;
     
        public void Packages () throws IOException
        {
            totalWeight = 0.0;
            shipment = new ArrayList<Packet>();
            Scanner fileScan;
     
            fileScan = new Scanner (new File("allPackets.txt"));
            // Read and process each line in the file
            while (fileScan.hasNext())
            {
                int id = fileScan.nextInt();
                String s = fileScan.next();
                double d = fileScan.nextDouble();
     
                Packet p = new Packet (id, s, d);
                shipment.add(p);
            }
            System.out.println(shipment);
        }
     
        public void displayLightPackages()
        {
            System.out.println("ALL LIGHT PACKAGES \n");
            for(Packet mypacket : shipment)
                if (mypacket.isLight())
                    System.out.println(mypacket);
        }
     
        public void displayHeavyPackages()
        {
            System.out.println("ALL HEAVY PACKETS \n");
            for(Packet mypacket : shipment)
                if (mypacket.isHeavy())
                    System.out.println(mypacket);
        }
     
        public void displayOtherPackages()
        {
            for(Packet mypacket : shipment)
                if(mypacket.isHeavy() || mypacket.isLight())
                    System.out.println("ALL REGULAR PACKETS \n");
                else
                    System.out.println(mypacket);  
        }
     
        public void displayTotalWeight()
        {
            for(Packet mypacket : shipment)
                totalWeight += mypacket.weight;
                System.out.println ("Total weight of all packets is " + totalWeight);
        }
     
        public void displayAverageWeight()
        {
            for(Packet mypacket : shipment)
            {
                totalWeight = shipment.size();
            }
            System.out.println("Average weight of all packets is " + totalWeight);
        }
     
        public String toString()
        {
            String s= "ALL PACKETS \n";
            for(Packet mypacket : shipment)
                s += mypacket;
            return s;
        }
     
    }

    public class TestPackages
    {
        public static void main(String[] args)
        {
            Packages mypacket = new Packages();
            System.out.println(mypacket);
        }
    }

    Any help on what I am doing wrong would be great! Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help getting this code to run...

    Please copy and paste the full text of the error message.
    If you know what line the variable with the null value is on, look at that line, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help getting this code to run...

    for(Packet mypacket : shipment)

    It says, java.lang.NullPointerException
    at Packages.toString(Packages.java:78)
    at java.lang.String.valueOf(String.java:2854)
    at java.io.PrintStream.println(PrintStream.java:821)
    at TestPackages.main(TestPackages.java:10)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help getting this code to run...

    Try debugging the code by adding some println() statements that print out the value of the shipment variable every time its value is changed and set to see why it has a null value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help getting this code to run...

    what will I be printing in...string s, int id, double d? in the array loop?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help getting this code to run...

    Just print the value of the shipment variable every time it is changed:
    System.out.println("s1="+shipment);
    change the digit to be unique in each call to println() so you can tell which println() printed the output.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help getting this code to run...

    I got the code to compile and run but it only outputs "ALL PACKETS" text...I don't know what's going on.

    public class Packet extends Packages
    {
        // instance variables - replace the example below with your own
        private int idNumber;
        private String state;
        public double weight;
     
        public Packet(int id, String s, double d)
        {
            this.idNumber = id;
            this.state = s;
            this.weight = d;
        }
     
        public boolean isHeavy()
        {
            if (weight > 12)
                return true;
            else
                return false;
        }
     
        public boolean isLight()
        {
            if (weight < 7)
                return true;
            else
                return false;
        }
     
        public String toString()
        {
            String s = "ID: " + idNumber + "\t" + "Weight: " + weight + "\t" + "State: " + state;
            return s;
        }
    }

    public class Packages
    {
        double totalWeight;
        public ArrayList<Packet> shipment = new ArrayList<Packet>();;
     
        public void Packages () throws IOException
        {
            totalWeight = 0.0;
            ArrayList<Packet> current = new ArrayList<Packet>();
            Scanner fileScan;
     
            fileScan = new Scanner (new File("allPackets.txt"));
            // Read and process each line in the file
            while (fileScan.hasNext())
            {
                int id = fileScan.nextInt();
                String s = fileScan.next();
                double d = fileScan.nextDouble();
     
                Packet p = new Packet (id, s, d);
                current.add(p);
            }
            System.out.println(current);
        }
     
        public void displayLightPackages()
        {
            System.out.println("ALL LIGHT PACKAGES \n");
            for(Packet mypacket : shipment)
                if (mypacket.isLight())
                    System.out.println(mypacket);
        }
     
        public void displayHeavyPackages()
        {
            System.out.println("ALL HEAVY PACKETS \n");
            for(Packet mypacket : shipment)
                if (mypacket.isHeavy())
                    System.out.println(mypacket);
        }
     
        public void displayOtherPackages()
        {
            for(Packet mypacket : shipment)
                if(mypacket.isHeavy() || mypacket.isLight())
                    System.out.println("ALL REGULAR PACKETS \n");
                else
                    System.out.println(mypacket);  
        }
     
        public void displayTotalWeight()
        {
            for(Packet mypacket : shipment)
                totalWeight += mypacket.weight;
                System.out.println ("Total weight of all packets is " + totalWeight);
        }
     
        public void displayAverageWeight()
        {
            for(Packet mypacket : shipment)
            {
                totalWeight = shipment.size();
            }
            System.out.println("Average weight of all packets is " + totalWeight);
        }
     
        public String toString()
        {
           String s = "ALL PACKETS \n";
           for(Packet mypacket : shipment)
                s += mypacket;
           return s;
        }
    }

    public class TestPackages
    {
        public static void main(String[] args)
        {
            Packages shipment = new Packages();
            System.out.println(shipment);
        }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help getting this code to run...

    I don't know what's going on.
    Try debugging the code by adding println() statements that print out the value of shipment at all locations in the code where the value of the variable: shipment is changed. You need to see if and when the value of and contents of shipment changes.
    See post#6
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help getting this code to run...

    I added this

    public String toString()
        {
           if (shipment == null)
               return "shipment is null";
           else{
           String s = "ALL PACKETS \n";
           for(Packet mypacket : shipment)
                s += mypacket;
           return s;}
        }

    It did return shipment is null.

    but whyyyy I read the file in right...I believe. I don't know if my array is set-up right.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help getting this code to run...

    What was printed out when you added all the println() calls I suggested to the code?
    You need to add lots of println() statements to see what the code is doing.
    The code you posted looks like it added just one println and that is NOT what I was suggesting as a place to add any.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help getting this code to run...

    Quote Originally Posted by Norm View Post
    What was printed out when you added all the println() calls I suggested to the code?
    You need to add lots of println() statements to see what the code is doing.
    The code you posted looks like it added just one println and that is NOT what I was suggesting as a place to add any.
    OK I figured it out...my array now prints the input file.

    BUT my methods (displayHeavyWeight, displayLightWeight...etc) are not being displayed or called I believe...how do I go about implementing that? When I run the output my strings do not show up...

     
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
     
    public class Packages
    {
        double totalWeight;
        public ArrayList<Packet> shipment;
     
        public Packages () throws IOException
        {
            totalWeight = 0.0;
            shipment = new ArrayList<Packet>();
            String currentPacket;
            Scanner fileScan, packetScan;
     
            fileScan = new Scanner (new File("allPackets.txt"));
            // Read and process each line in the file
            while (fileScan.hasNext())
            {
                currentPacket = fileScan.nextLine();
                packetScan = new Scanner (currentPacket);
     
                int id = packetScan.nextInt();
                double d = packetScan.nextDouble();
                String s = packetScan.next();
     
                Packet temp = new Packet (id,d,s);
                shipment.add(temp);
            }
            System.out.println(shipment);
        }
     
        public void displayLightPackages()
        {
            System.out.println("ALL LIGHT PACKAGES \n");
            for(Packet thepacket : shipment)
                if (thepacket.isLight())
                    System.out.println(thepacket);
        }
     
        public void displayHeavyPackages()
        {
            System.out.println("ALL HEAVY PACKETS \n");
            for(Packet thepacket : shipment)
                if (thepacket.isHeavy())
                    System.out.println(thepacket);
        }
     
        public void displayOtherPackages()
        {
            for(Packet thepacket : shipment)
                if(thepacket.isHeavy() || thepacket.isLight())
                    System.out.println("ALL REGULAR PACKETS \n");
                else
                    System.out.println(thepacket);  
        }
     
        public void displayTotalWeight()
        {
            for(Packet thepacket : shipment)
                totalWeight += thepacket.weight;
                System.out.println ("Total weight of all packets is " + totalWeight);
        }
     
        public void displayAverageWeight()
        {
            for(Packet thepacket : shipment)
            {
                totalWeight = shipment.size();
            }
            System.out.println("Average weight of all packets is " + totalWeight);
        }
     
        public String toString()
        {
               String s = "ALL PACKETS \n";
               for(Packet thepacket : shipment)
               s += thepacket + "\n";
               return s;
        }  
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help getting this code to run...

    my strings do not show up...
    Which methods are you calling? Do each of them have a println() at the start of the method?

    To call a method, you must have a reference to an instance of the class with the method and use that:
    TheClass aRefToClass = new TheClass();
     ....
    aRetfToClass.theMethodToCall(): // call a method in TheClass
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to run a C++ code using java?
    By progfool in forum Java Native Interface
    Replies: 5
    Last Post: August 2nd, 2013, 06:46 PM
  2. My code will not run like I want it to. What is wrong???
    By koolestkid20 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 24th, 2012, 08:35 PM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. unable to run log code
    By javaking in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 6th, 2010, 11:49 PM
  5. How to run this code?
    By jackdear44 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 11:37 PM