Results 1 to 4 of 4

Thread: Help please

  1. #1
    dawesbs is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default Help please

    im getting a null pointer exception and i really dont know why heres my code and the error message if anyone can help that would be great

    Java Code:
    package lab08;
    
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            PriceTable priceTable = new PriceTable();
            Customer customer = new Customer();
            int zone;
            double price, packageWeight, costOfShipping, newZonePrice;
            char choice;
            double[] prices;
            priceTable.LoadPrices();
            choice = JOptionPane.showInputDialog("Enter Choice( L,S,C,Q):").charAt(0);
            while (choice != 'Q') {
                if (choice == 'L') {
                    customer.LoadZone();
                    zone = customer.GetZone();
                    JOptionPane.showMessageDialog(null, "The Price per Pound for Zone" + zone + " is" + priceTable.GetPrices(zone));
                } else if (choice == 'S') {
                    customer.LoadCustomerInfo();
                    customer.LoadShipToInfo();
                    customer.LoadZone();
                    customer.LoadWeight();
                    zone = customer.GetZone();
                    packageWeight = customer.GetWeight();
                    price = priceTable.GetPrices(zone);
                    costOfShipping = price * packageWeight;
                    customer.ProduceShippingLabel(costOfShipping);
    
                } else if (choice == 'C') {
                    customer.LoadZone();
                    zone = customer.GetZone();
                    newZonePrice = Integer.parseInt(JOptionPane.showInputDialog("Enter New Price for Zone " + zone));
                    priceTable.UpdatePriceFile(zone, newZonePrice);
                } else {
                    JOptionPane.showMessageDialog(null, "Invalid Choice - Try Again");
                }
    
                choice = JOptionPane.showInputDialog("Enter Choice( L,S,C,Q):").charAt(0);
            }
    
            priceTable.SavePrices();
    
            System.exit(0);
        }
    }

    run:
    Exception in thread "main" java.lang.NullPointerException
    at lab08.PriceTable.LoadPrices(PriceTable.java:12)
    at lab08.Main.main(Main.java:14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by dawesbs; 11-17-2009 at 08:08 AM. Reason: forgot tags

  2. #2
    dawesbs is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    oh and here is the class that the error is in

    Java Code:
    package lab08;
    import java.io.*;
    import java.util.Scanner;
    
    public class PriceTable {
        private double[] zonePrices;
    
        public void LoadPrices()throws IOException{
            File zoneFileHandle = new File("Zone Prices.txt");
            Scanner zoneFile = new Scanner(zoneFileHandle);
            for(int cnt=0; cnt<15; cnt++){
                zonePrices[cnt]=zoneFile.nextDouble();
            }
            zoneFile.close();
        }
    
        public double GetPrices(int zone){
            return (zonePrices[zone]);
        }
    
        public void UpdatePriceFile(int zone, double newZonePrice){
            zonePrices[zone] = newZonePrice;
            
    
        }
        
        public void SavePrices()throws IOException{
            File zoneFileHandle = new File("Zone Prices.txt");
            PrintWriter zoneFile = new PrintWriter(zoneFileHandle);
            for(int cnt=0; cnt<15; cnt++)
                zoneFile.println(zonePrices[cnt]);
        }
    
    }
    Last edited by dawesbs; 11-17-2009 at 08:08 AM. Reason: forgot tags

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    private double[] zonePrices; does not create an array. It merely creates a reference that points to nothing (null). You need to initialize that variable with something like
    Java Code:
    zonePrices = new double[someIntegerValue];
    before you do any of the

    zonePrices[cnt]=zoneFile.nextDouble(); stuff.

  4. #4
    dawesbs is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •