数组中的数组奇怪的事情?JAVA
我有一个变量数组中的数组奇怪的事情?JAVA,java,arrays,Java,Arrays,我有一个变量B,用于存储在数组userarr中添加了多少用户对象。我使用的一行代码添加了一条注释,用户在userarr数组中的B元素user对象中键入注释。这是: userarr[login].notes[b] = tempnote; tempnote是一个字符串变量,临时保存用户键入的便笺,login存储您登录的用户号码。因此,它将字符串变量tempnote分配给user数组中的loginvalue用户对象,并在notes数组中为该用户分配元素b元素 但是由于某种原因,这行代码有一个问题。
B
,用于存储在数组userarr
中添加了多少用户对象。我使用的一行代码添加了一条注释,用户在userarr
数组中的B
元素user对象中键入注释。这是:
userarr[login].notes[b] = tempnote;
tempnote
是一个字符串变量,临时保存用户键入的便笺,login
存储您登录的用户号码。因此,它将字符串变量tempnote
分配给user
数组中的login
value用户对象,并在notes数组中为该用户分配元素b
元素
但是由于某种原因,这行代码有一个问题。
我知道是那一行代码,因为它发生在你确认要添加注释之后,并且在它旁边有一个永远不会出现的println
下面是整个音符的方法:
public static void loggedin() {
System.out.println("welcome, " + userarr[login].username + "!");
do{
System.out.println("type 'notes' to list current notes, type 'new' to add notes, type 'delete' to delete a note, or type 'exit' to exit the program.");
switch(scan.nextLine()){
case "exit":
System.out.println("exiting");
login = -1;
break;
case "delete":
break;
case "new":
System.out.println("\n\nType the note.");
String tempnote = scan.nextLine();
System.out.println("note is is now " + tempnote + ". is this what you want? type 'yes' to proceed, or 'no' to enter note again.");
String ch5 = scan.nextLine();
if (ch5.equals("no")) {
break;
} else {
userarr[login].notes[b] = tempnote;
System.out.println("note created!");
b += 1;
}
break;
case "notes":
for (int i=0;i<b;i++) {
System.out.println("Note " + i + ":");
System.out.println(userarr[login].notes[i] + "\n");
}
break;
default:
System.out.println("restarting.");
};
}while(login != -1);
}
在我运行它之前没有错误。当我谈到有问题的部分时,它会说:
Exception in thread "main" java.lang.NullPointerException
at text.game.TextGame.loggedin(TextGame.java:80)
at text.game.TextGame.login(TextGame.java:53)
at text.game.TextGame.main(TextGame.java:135)
有人知道这个问题吗
编辑:似乎有必要展示整个课堂,因为显然有很多信息人们需要知道。因此,以下是所有您需要的信息:
package text.game;
import java.util.Scanner;
public class TextGame {
static Scanner scan = new Scanner(System.in);
static class user extends TextGame {
String username;
int password;
String[] notes;
public user(String username, int password) {
this.username = username;
this.password = password;
}
}
static user[] userarr = new user[10];
static int a = 0;
static int b = 0;
static int login = -1;
public static void newuser() {
System.out.println("\n\nType the username for this user.");
String usernameA = scan.nextLine();
System.out.println("Username is now " + usernameA + ". is this what you want? type 'yes' to proceed, or 'no' to enter username again.");
if (scan.nextLine().equals("no")) {
newuser();
} else {
System.out.println("\n\n type the password for this user. (numbers only.)");
int passwordA = scan.nextInt();
System.out.println("user is " + usernameA + " and password is " + passwordA + ". creating user.");
userarr[a] = new user(usernameA, passwordA);
System.out.println("user created!");
a += 1;
}
}
public static void login() {
System.out.println("which account do you want to log into? type the name of a user, or type 'list' to view the users signed in.");
String ch2 = scan.nextLine();
if (ch2.equals("list")){
for (int i=0;i<a;i++) {
System.out.println(userarr[i].username);
}
} else {
for (int i=0;i<a;i++) {if ((userarr[i].username).equals(ch2)){
System.out.println("type the password for this account (USE NUMBERS ONLY).");
int ch4 = scan.nextInt();
if (ch4==userarr[i].password) {
System.out.println("logged in!"); login = i; loggedin();
}else{
System.out.print("incorrect password!");
}
}
}
}
}
public static void loggedin() {
System.out.println("welcome, " + userarr[login].username + "!");
do{
System.out.println("type 'notes' to list current notes, type 'new' to add notes, type 'delete' to delete a note, or type 'exit' to exit the program.");
switch(scan.nextLine()){
case "exit":
System.out.println("exiting");
login = -1;
break;
case "delete":
break;
case "new":
System.out.println("\n\nType the note.");
String tempnote = scan.nextLine();
System.out.println("note is is now " + tempnote + ". is this what you want? type 'yes' to proceed, or 'no' to enter note again.");
String ch5 = scan.nextLine();
if (ch5.equals("no")) {
break;
} else {
userarr[login].notes[b] = tempnote;
System.out.println("note created!");
b += 1;
}
break;
case "notes":
for (int i=0;i<b;i++) {
System.out.println("Note " + i + ":");
System.out.println(userarr[login].notes[i] + "\n");
}
break;
default:
System.out.println("restarting.");
};
}while(login != -1);
}
public static void main(String[] args) {
do {
System.out.println("Welcome to LINCOLN COMP console OS. Type 'new' to create a new user, type 'log' to log in to an existing user, or type 'exit' to leave.\nif you are asked a yes or no question, if you type something other than yes or no, it will default to yes.");
String ch1 = scan.nextLine();
switch (ch1) {
case "new":
if (a==2) {
System.out.println("maximum users have been created. type a username to delete that user, type list to list users, or type back to return.");
String ch3 = scan.nextLine();
if (ch3.equals("list")) {
for (int i=0;i<a;i++) {
if (userarr[i].username==null) {
System.out.println("undefined");
}else{
System.out.println("\n" + userarr[i].username);
};
}
} else if (ch3.equals("back")) {
break;
} else {
for (int i=0;i<a;i++) {
if ((userarr[i].username).equals(ch3)){
System.out.println("type the password for this account (USE NUMBERS ONLY).");
int ch4 = scan.nextInt();
if (ch4==userarr[i].password) {
a --;
userarr[i] = null;
System.out.println("user deleted!");
break;
}else{
System.out.println("incorrect password.");
break;
}
}else if (i==a-1) {
System.out.println("user not found.");
break;
}
}
}
}else {
System.out.println("Initializing user creation method:");
newuser();
}
break;
case "log":
login();
break;
case "exit":
System.out.println("Goodbye!");
System.exit(0);
break;
case "debug":
for (int i=0;i<userarr.length;i++) {
System.out.println(userarr[i]);
}
break;
default:
System.out.println("restarting.");
}
} while (true);
}
}//Note from other user - Extra bracket?
package text.game;
导入java.util.Scanner;
公共类文本游戏{
静态扫描仪扫描=新扫描仪(System.in);
静态类用户扩展文本游戏{
字符串用户名;
int密码;
字符串[]注释;
公共用户(字符串用户名、整数密码){
this.username=用户名;
this.password=密码;
}
}
静态用户[]userarr=新用户[10];
静态int a=0;
静态int b=0;
静态int-login=-1;
公共静态void newuser(){
System.out.println(“\n\n键入此用户的用户名。”);
字符串usernameA=scan.nextLine();
System.out.println(“用户名现在是“+usernameA+”。这是您想要的吗?键入“是”继续,或键入“否”再次输入用户名。”);
if(scan.nextLine().equals(“no”)){
newuser();
}否则{
System.out.println(“\n\n键入此用户的密码。(仅限数字”);
int passwordA=scan.nextInt();
System.out.println(“用户为“+usernameA+”,密码为“+passwordA+”。正在创建用户”);
userarr[a]=新用户(usernameA,passwordA);
System.out.println(“用户创建!”);
a+=1;
}
}
公共静态void登录(){
System.out.println(“您想登录哪个帐户?键入用户名,或键入'list'查看登录的用户。”);
字符串ch2=scan.nextLine();
如果(ch2等于(“列表”)){
对于(inti=0;i变化
例如:
String[] notes = new String[10];
我注意到,在使用扫描仪的下一个功能时,您似乎没有检查是否有下一行/整数
以代码中的这两行为例
String ch2 = scan.nextLine();
int ch4 = scan.nextInt();
您可以使用hasNext函数检查是否确实存在下一个int或下一行
String ch2; //Declared outside of the if-statement for scope reasons
if(scan.hasNextLine()){
//the hasNext functions return true if there is a next line (or int for hasNextInt).
ch2 = scan.nextLine();
}else{
//if something needs to be done in the event that there is no nextLine, do it here
}
int ch4;
if(scan.hasNextInt()){
ch4 = scan.hasNextInt();
}else{
//if something needs to be done in the event that there is no nextInt, do it here
}
这可能无法解决您的问题;但是,这至少可以防止以后出现许多潜在的问题。您应该始终确保在获取内容之前有更多的内容可获取,并且使用hasNext函数可以使其变得非常简单
查看更多信息,请参见哪一行是TextGame:80?您是否附加了调试器并进行了调试?空指针通常非常简单。您没有提供足够的信息。但是,错误会告诉您问题的具体内容和位置。位于text.game.TextGame.loggedin(TextGame.java:80)
TextGame.java的第80行。ANullPointerException
表示该行中的某些内容未初始化。这就是我所说的有问题的那一行,userarr[login]。注意事项[b]=tempnote;什么是扫描对象?它的数据类型是什么?我不知道。它是我用import java.util.Scanner;导入的java实用程序。它允许用户键入内容。
String ch2 = scan.nextLine();
int ch4 = scan.nextInt();
String ch2; //Declared outside of the if-statement for scope reasons
if(scan.hasNextLine()){
//the hasNext functions return true if there is a next line (or int for hasNextInt).
ch2 = scan.nextLine();
}else{
//if something needs to be done in the event that there is no nextLine, do it here
}
int ch4;
if(scan.hasNextInt()){
ch4 = scan.hasNextInt();
}else{
//if something needs to be done in the event that there is no nextInt, do it here
}