NullPointerException,我无法弄清楚什么是null值

时间:2015-12-11 07:53:54

标签: java

我是一名大学生,这段代码是我在Java课程介绍中的一个项目。我一遍又一遍地使用它,在stackoverflow和其他网站上搜索答案,但我仍然无法弄清楚问题是什么。该程序旨在通过提示输入名字和姓氏以及电子邮件地址来存储客户端。客户端将获得客户端ID,他们可以使用它来预订存储单元或显示已预订的存储单元。当我运行程序时,我得到了这个例外:

Exception in thread "main" java.lang.NullPointerException
    at Webstore3.WebStorage.Sub_Menu(WebStorage.java:153)
    at Webstore3.WebStorage.main(WebStorage.java:85)

我已经在stackoverflow上看到了关于NullPointerExceptions的问题和答案,并且我已经完成了测试代码以确保输入 customer [i] .Client_ID < / strong>,但我还没有意识到问题所在。我知道代码并不完美,但是在获得第一个错误之后的任何和所有帮助都将非常感激。

    package Webstore3;

    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.JOptionPane;
    import java.lang.StringBuilder;

    public class WebStorage
    {   
        static int c = 0;
        static int b = 0;
        static int s = 0;
        static WebStorage temp_w = new WebStorage();
        static Customer temp_c;
        static Booking temp_b;
        static Storage temp_s;
        static int i = 0;
        static int j = 0;
        static int k = 0;
        static int Confirm;
        static int Choose;
        static String input = new String("");
        static int temp = 0;
        static String temp_string = "";
        static String temp_string2 = "";
        static Random r = new Random();
        static Customer[] customer = new Customer[100];
        static Booking[] booking = new Booking[14];
        static Storage[] storage = new Storage[14];
        //static ArrayList<Customer> customer2 = new ArrayList<Customer>();
        //static ArrayList<Customer> booking2 = new ArrayList<Customer>();
        //static ArrayList<Customer> storage2 = new ArrayList<Customer>();
        static int w = 0;
        static int l = 0;


        public static void main(String[] args) 
        {

            Confirm = JOptionPane.showConfirmDialog(null, "Would you like to Access ~WebStorage~?");
            if(Confirm == 0)
            {
                while(temp != 3)
                {
                    temp = MainMenu();

                    if(temp == 1)
                    {
                        temp_c = new Customer();
                        temp_c.First_Name();
                        temp_c.Last_Name();
                        temp_c.Email();
                        temp_string = temp_c.Account_Created();
                        customer[c] = temp_c;

                        if(Confirm == 0 )
                        {
                            c++;
                            input = JOptionPane.showInputDialog("What would you like to do now?" + "\n" + "\n" + "Book Unit - (1)" + "\n" + "Main Menu - (2)");
                            Choose = Integer.parseInt(input);
                            if(Choose == 1)
                            {
                                input = JOptionPane.showInputDialog("Enter unit width: ");
                                w = Integer.parseInt(input);

                                input = JOptionPane.showInputDialog("Enter unit length: ");
                                l = Integer.parseInt(input);

                                temp_s = new Storage();
                                temp_string2 = temp_s.Storage(w, l);
                                storage[s] = temp_s;
                                if(temp_string2 != "")
                                {
                                    temp_b = new Booking();
                                    temp_b.Storage_ID = temp_string2;
                                    temp_b.Client_ID = temp_string;
                                    booking[b] = temp_b;
                                    b++;
                                }
                            }
                        }
                    }
                    if(temp == 2)
                    {
                        Sub_Menu();
                    }
                    if(temp == 3)
                    {
                        JOptionPane.showMessageDialog(null, "Goodbye");
                        System.exit(0);
                    }
                }

            }
            if(Confirm == 1)
            {
                System.out.println("Goodbye");
                System.exit(0);
            }
        }

        public static int MainMenu()
        {
            input = JOptionPane.showInputDialog("Please enter the number correspoding to your desired action: " + "\n" + "\n" + "Create an Account - (1)" + "\n" + "Booking - (2)" + "\n" + "Quit Program - (3)" +"\n");
            temp = Integer.parseInt(input);
            return temp;
        }

        public static void Sub_Menu()
        {
            input = JOptionPane.showInputDialog("Please enter the number correspoding to your desired action: " + "\n" + "\n" + "Book Storage - (1)" + "\n" + "Search Booking - (2)" + "\n" + "Display all Booking - (3)" +"\n" + "Main Menu - (4)");
            Choose = Integer.parseInt(input);

            if(Choose == 1)
            {
                input = JOptionPane.showInputDialog("Enter your Client ID: ");
                temp_string = input;

                for(i = 0; i < customer.length; i++)
                {
                    if(input == customer[i].Client_ID)
                    {
                        input = JOptionPane.showInputDialog("Enter unit width: ");
                        w = Integer.parseInt(input);

                        input = JOptionPane.showInputDialog("Enter unit length: ");
                        l = Integer.parseInt(input);

                        temp_s = new Storage();
                        temp_string2 = temp_s.Storage(w, l);
                        storage[s] = temp_s;
                        if(temp_string2 != "")
                        {
                            temp_b = new Booking();
                            temp_b.Storage_ID = temp_string2;
                            temp_b.Client_ID = temp_string;
                            booking[b] = temp_b;
                            b++;
                        }
                    }
                }

                JOptionPane.showMessageDialog(null, "Client ID not found!" + "\n" + "\n" + "Press ok, to return to the main menu and create an account");
            }
            if(Choose == 2)
            {
                input = JOptionPane.showInputDialog("Enter your Client ID: ");


                for(i = 0; i < customer.length; i++)
                {
                    if(input == customer[i].Client_ID)
                    {
                        input = JOptionPane.showInputDialog("Enter your Storage ID: ");

                        for(i = 0; i < storage.length; i++)
                        {
                            if(input == storage[i].Storage_ID)
                            {
                                JOptionPane.showMessageDialog(null, "size: " + storage[i].sb2 + "\n" + "Rental: " + storage[i].price_h + "\n" + "Storage ID: " + storage[i].sb + "\n" + "\n" + "Press ok, to return to the Main menu");
                            }
                        }
                    }
                }
            }
            if(Choose == 3)
            {
                input = JOptionPane.showInputDialog("Enter you Client ID: ");

                for(i = 0; i < customer.length; i++)
                {
                    if(input == customer[i].Client_ID)
                    {
                        for(j = 0; j < booking.length; j++)
                        {
                            if(input == booking[j].Client_ID)
                            {
                                for(k = 0; k < storage.length; k++)
                                {
                                    if(booking[j].Storage_ID == storage[k].Storage_ID)
                                    {
                                        JOptionPane.showMessageDialog(null, "First Name: " + customer[i].Client_FirstName + "\n" + "Last Name: " + customer[i].Client_LastName + "\n"+ "Email: " + customer[i].Client_Email + "\n" + "size: " + storage[k].sb2 + "\n" + "Rental: " + storage[k].price_h + "\n" + "Storage ID: " + storage[k].sb + "\n" + "\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if(Choose == 4)
            {

            }


        }

    }

    class Customer extends WebStorage
    {
        static String input = new String("");
        static boolean error;
        static int Confirm = 0;
        static int Valid_Email = 0;
        static int Valid_ID = 0;
         String Client_FirstName = "";
         String Client_LastName = "";
         String Client_Number = "";
         String Client_Email = "";
        static int[] temp_array = new int[9];
        static int[] Client_ID_Array = new int[9];
         String Client_ID = ""; 
         StringBuilder sb;

         Customer()
            {
                Client_FirstName = new String("");
                Client_LastName = new String("");
                Client_Number = new String("");
                Client_Email = new String("");
                Client_ID = new String("");
                sb = new StringBuilder("");
            }

            Customer(String Client_FirstName, String Client_LastName, String Client_Number, String Client_Email, String Client_ID, StringBuilder sb)
            {
                this.Client_FirstName = Client_FirstName;
                this.Client_LastName = Client_LastName;
                this.Client_Number = Client_Number;
                this.Client_Email = Client_Email;
                this.Client_ID = Client_ID;
                this.sb = sb;


            }

        public void First_Name()
        {
            input = JOptionPane.showInputDialog("Enter your Information to create an account: " + "\n" + "\n" + "First Name: ");
            for(int i = 0; i < input.length(); i++)
            {
                error = Character.isLetter(input.charAt(i));
                if(error == false)
                {
                    input = JOptionPane.showInputDialog("Your name may only contain letters" + "\n" + "\n" + "First Name: ");
                    error = Character.isLetter(input.charAt(i));
                    i = -1;
                }
            }
            Client_FirstName = input;
        }

        public void Last_Name ()
        {
            input = JOptionPane.showInputDialog("Enter your Information to create an account: " + "\n" + "\n" + "Last Name: ");
            for(int i = 0; i < input.length(); i++)
            {
                error = Character.isLetter(input.charAt(i));
                if(error == false)
                {
                    input = JOptionPane.showInputDialog("Your name may only contain letters" + "\n" + "\n" + "Last Name: ");
                    error = Character.isLetter(input.charAt(i));
                    i = -1;
                }
            }
            Client_LastName = input;
        }

        public void Email ()
        {
            input = JOptionPane.showInputDialog("Enter your Information to create an account: " + "\n" + "\n" + "Email: (Must contain a @, and end with .com, .net, .org, or .edu) ");
            while(Valid_Email < 2)
            {
                Valid_Email = 0;

                error = input.contains("@");
                if(error == true)
                {
                    Valid_Email++;
                }
                error = input.contains(".com");
                if(error == true)
                {
                    Valid_Email++;
                }
                error = input.contains(".net");
                if(error == true)
                {
                    Valid_Email++;
                }
                error = input.contains(".org");
                if(error == true)
                {
                    Valid_Email++;
                }
                error = input.contains(".edu");
                if(error == true)
                {
                    Valid_Email++;
                }

                if(Valid_Email < 2)
                {
                    input = JOptionPane.showInputDialog("Invalid email entered! " + "\n" + "\n" + "Email: (Must contain a @, and end with .com, .net, .org, or .edu)  ");
                }
            }

            Client_Email = input;

        }

        public String Account_Created()
        {
            Confirm = JOptionPane.showConfirmDialog(null, "Account Created!" + "\n" + "Is everything correct?" + "\n" + "First Name: " + Client_FirstName + "\n" + "Last Name: " + Client_LastName + "\n"+ "Email: " + Client_Email);
            if(Confirm == 0)
            {
                do{
                    for(i = 0; i < temp_array.length; i++)
                    {
                        temp_array[i] = r.nextInt(9);
                    }
                    for(i = 0; i < customer.length; i++)
                    {
                        for(int j = 0; j < Client_ID_Array.length; j++)
                        {
                            if(temp_array[j] == customer[i].Client_ID_Array[j])
                            {
                                Valid_ID++;
                            }
                        }
                    }
                }while(Valid_ID == 9);
                Client_ID_Array = temp_array;
                for(i = 0; i < Client_ID_Array.length; i++)
                {
                    sb.append(Client_ID_Array[i]);
                }
                Client_ID = sb.toString();
                JOptionPane.showMessageDialog(null, "Your Client ID#: " + Client_ID );
            }

            return Client_ID;
        }


    }

    class Booking extends WebStorage
    {
         String Client_ID;
         String Storage_ID;
        static int i = 0;
         String temp_string = "";
         String input = "";
        static int Choose = 0;
        static int Choose2 = 0;
        static int w = 0;
        static int l = 0;


        Booking()
        {
            Client_ID = "";
            Storage_ID = "";
            temp_string = "";
            input = "";
        }

        Booking(String Client_ID, String Storage_ID, String temp_string, String input)
        {
            this.Client_ID = Client_ID;
            this.Storage_ID = Storage_ID;
            this.temp_string = temp_string;
            this.input = input;
        }


    }

    class Storage extends WebStorage
    {
        static int Confirm = 0;
        static int[] width  = { 5,  5, 10, 10,  10,  10,  10};
        static int[] length = { 5, 10, 10, 15,  20,  25,  30};
        static int[] unit   = { 3,  3,  2,  2,   2,   1,   1};
        static int[] price  = {16, 33, 41, 73, 163, 245, 277};
        static int price_h = 0;
        static int total_units = 1;
         String Storage_ID = "";
         StringBuilder sb;
         StringBuilder sb2;
        static char by = 'x';

        Storage()
        {
            Storage_ID = "";
            sb = new StringBuilder();
            sb2 = new StringBuilder();
        }

        Storage(String Storage_ID, StringBuilder sb, StringBuilder sb2)
        {
            this.Storage_ID = Storage_ID;
            this.sb = sb;
            this.sb2 = sb2;
        }


        public String Storage(int w, int l)
        {
            for(int i = 0; i < width.length; i++)
            {
                if(w == width[i] && l == length[i] && unit[i] > 0)
                {
                        if(total_units < 10)
                        {
                            sb.append(0);
                            sb.append(total_units);
                        }
                        if(total_units > 9)
                        {
                            sb.append(total_units);
                        }
                        if(width[i] < 10)
                        {
                            sb.append(0);
                            sb.append(w);
                            if(length[i] < 10)
                            {
                                sb.append(0);
                                sb.append(l);
                            }
                            if(length[i] > 9)
                            {
                                sb.append(l);
                            }
                        }
                        if(width[i] > 9)
                        {
                            sb.append(w);
                            if(length[i] < 10)
                            {
                                sb.append(0);
                                sb.append(l);
                            }
                            if(length[i] > 9)
                            {
                                sb.append(l);
                            }
                        }
                    unit[i] = unit[i] - 1;
                    total_units = total_units + 1;

                    sb2.append(w);
                    sb2.append(by);
                    sb2.append(l);

                    price_h = price[i];

                    JOptionPane.showMessageDialog(null, "Storage Booked!" + "\n" + "\n" + "size: " + sb2 + "\n" + "Rental: " + price_h + "\n" + "Storage ID: " + sb );

                    Storage_ID = sb.toString();

                    WebStorage.s++;

                }
                if((w != width[i] && l !=length[i]) && unit[i] <= 0)
                {
                    JOptionPane.showMessageDialog(null, "No storage units of that size are available");
                }
                if(total_units == 14)
                {
                    JOptionPane.showMessageDialog(null, "All storage units are occupied!");
                }
            }

            return Storage_ID;
        }

1 个答案:

答案 0 :(得分:0)

看看这些要点。

您声明了一个大小为100的static Customer[] customer = new Customer[100]; 数组。

for(i = 0; i < customer.length; i++)

稍后在代码中循环遍历所有数组条目

i

但您不检查索引Customer是否已存储NUllPointerException的引用。如果没有,你会得到if(input == customer[i].Client_ID)

std::size_t