Java NullPointerException,我不知道是什么给出了空值
我是一名大学生,这段代码是为我的Java入门课程中的一个项目编写的。我一次又一次地使用它,在stackoverflow和其他网站上搜索答案,但我仍然不知道问题出在哪里。该程序旨在通过提示输入姓名和电子邮件地址来存储客户端。客户机将获得一个客户机ID,他们可以使用该ID预订存储单元或显示预订的存储单元。当我运行该程序时,会出现以下异常:Java NullPointerException,我不知道是什么给出了空值,java,Java,我是一名大学生,这段代码是为我的Java入门课程中的一个项目编写的。我一次又一次地使用它,在stackoverflow和其他网站上搜索答案,但我仍然不知道问题出在哪里。该程序旨在通过提示输入姓名和电子邮件地址来存储客户端。客户机将获得一个客户机ID,他们可以使用该ID预订存储单元或显示预订的存储单元。当我运行该程序时,会出现以下异常: Exception in thread "main" java.lang.NullPointerException at Webstore3.WebSto
Exception in thread "main" java.lang.NullPointerException
at Webstore3.WebStorage.Sub_Menu(WebStorage.java:153)
at Webstore3.WebStorage.main(WebStorage.java:85)
我已经看到了有关NullPointerException的stackoverflow的问题和答案,并且我已经完成了测试代码,以确保输入和客户[I].Client_ID中都存在一些问题,但我还没有意识到问题是什么。我知道这段代码并不完美,但如果您能帮助我们克服第一个错误,我们将不胜感激
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;
}
packagewebstore3;
导入java.util.ArrayList;
导入java.util.Random;
导入javax.swing.JOptionPane;
导入java.lang.StringBuilder;
公共类网络存储
{
静态int c=0;
静态int b=0;
静态int s=0;
静态WebStorage temp_w=新WebStorage();
静态客户温度;
静态预订温度;
静态存储温度;
静态int i=0;
静态int j=0;
静态int k=0;
静态int确认;
静态整数选择;
静态字符串输入=新字符串(“”);
静态温度=0;
静态字符串temp_String=“”;
静态字符串temp_string2=“”;
静态随机r=新随机();
静态客户[]客户=新客户[100];
静态预订[]预订=新预订[14];
静态存储[]存储=新存储[14];
//静态ArrayList customer2=新的ArrayList();
//静态ArrayList booking2=新建ArrayList();
//静态ArrayList storage2=新的ArrayList();
静态int w=0;
静态int l=0;
公共静态void main(字符串[]args)
{
Confirm=JOptionPane.showConfirmDialog(null,“您想访问~WebStorage~?”吗);
如果(确认==0)
{
while(温度!=3)
{
temp=main菜单();
如果(温度==1)
{
temp_c=新客户();
临时姓名();
临时姓氏();
临时电子邮件();
temp_string=temp_c.Account_Created();
客户[c]=临时工;
如果(确认==0)
{
C++;
input=JOptionPane.showInputDialog(“您现在想做什么?”+“\n”+“\n”+“书本单元-(1)”+“\n”+“主菜单-(2)”);
Choose=Integer.parseInt(输入);
如果(选择==1)
{
input=JOptionPane.showInputDialog(“输入单位宽度:”);
w=整数.parseInt(输入);
input=JOptionPane.showInputDialog(“输入单位长度:”);
l=整数.parseInt(输入);
temp_s=新存储器();
温度字符串2=温度存储(w,l);
存储=温度;
如果(临时字符串2!=“”)
{
temp_b=新预订();
临时存储ID=临时字符串2;
temp_b.Client_ID=temp_字符串;
预订[b]=临时工;
b++;
}
}
}
}
如果(温度=2)
{
子菜单();
}
如果(温度=3)
{
showMessageDialog(null,“再见”);
系统出口(0);
}
}
}
如果(确认==1)
{
System.out.println(“再见”);
系统出口(0);
}
}
公共静态int主菜单()
{
input=JOptionPane.showInputDialog(“请输入与所需操作相对应的号码:”+“\n”+“\n”+”创建帐户-(1)”+“\n”+“预订-(2)”+“\n”+“退出程序-(3)”+“\n”);
temp=整数.parseInt(输入);
返回温度;
}
公共静态无效子菜单()
{
input=JOptionPane.showInputDialog(“请输入与所需操作相对应的号码:”+“\n”+“\n”+“图书存储-(1)”+“\n”+“搜索预订-(2)”+“\n”+“显示所有预订-(3)”+“\n”+“主菜单-(4)”);
Choose=Integer.parseInt(输入);
如果(选择==1)
{
input=JOptionPane.showInputDialog(“输入您的客户端ID:”);
临时字符串=输入;
对于(i=0;istatic Customer[] customer = new Customer[100];
for(i = 0; i < customer.length; i++)
if(input == customer[i].Client_ID)