数组和类的空指针异常-Java
好的,我有一个全局数组作为类“Temp”的一部分。我想在程序中的任何地方编辑该数组。这很好。然而,当我试图使用这些值在我的“Cords”类中设置值时,我得到了一个Null异常错误。 我已经在我的代码中对其进行了注释。知道为什么吗数组和类的空指针异常-Java,java,arrays,class,global-variables,Java,Arrays,Class,Global Variables,好的,我有一个全局数组作为类“Temp”的一部分。我想在程序中的任何地方编辑该数组。这很好。然而,当我试图使用这些值在我的“Cords”类中设置值时,我得到了一个Null异常错误。 我已经在我的代码中对其进行了注释。知道为什么吗 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner inp = new Scanner(S
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int n = 0;
for(int k = 0; k<4; k++){
Temp.tempValues[k] = 0;
}
boolean checkNums = false;
String coords = "";
while(n<1 || n>2000){
System.out.println("Enter number of lines");
n = inp.nextInt();
}
Cords[] lines = new Cords[n];
int proceed = 0;
inp.nextLine();
for(int i = 0; i<n; i++){
proceed = 0;
checkNums = false;
while((proceed != 3) || (checkNums == false)){
System.out.println("Enter line coordinates. "+(i+1));
coords = inp.nextLine();
proceed = checkSpaces(coords);
checkNums = rightNums(coords);
}
lines[i] = new Cords();
lines[i].setValues(Temp.tempValues[0], Temp.tempValues[1], Temp.tempValues[2], Temp.tempValues[3]);
}
}
public static int checkSpaces(String sent){
int spaces = 0;
for(int y = 0; y< sent.length(); y++){
if(sent.charAt(y)==' '){
spaces++;
}
}
return spaces;
}
public static boolean rightNums(String sent){
int z = 0;
int l = 0;
String num = "";
while(z<sent.length()){
while((z<sent.length())&&(sent.charAt(z) != ' ')){
num += sent.charAt(z);
z++;
}
if(Integer.parseInt(num) < 0 || Integer.parseInt(num) >=10000){
return false;
}else{
Temp.tempValues[l] = Integer.parseInt(num);
num = "";
z++;
l++;
}
}
return true;
}
public class Cords{
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
public void setValues(int xx1, int yy1, int xx2, int yy2){
x1 = xx1;
y1 = yy1;
x2 = xx2;
y2 = yy2;
}
}
public static class Temp{
static int[] tempValues = new int[4];
}
}
import java.util.Scanner;
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪inp=新扫描仪(System.in);
int n=0;
对于(int k=0;k您的行
数组不包含任何对象。它只包含null
s,您不能调用null
-值上的方法
由于您正在迭代行
数组,请在调用当前元素上的方法之前对其进行初始化:
for (int i = 0; i < lines.length; i++) {
lines[i] = new Cords();
lines[i].setValues(...);
}
for(int i=0;i
您还必须使跳线
类保持静态。您的行
数组不包含任何对象。它只包含null
s,您不能调用null
-值上的方法
public static void main(String[] args){
Cords[] lines = new Cords[5];
for(int k = 0; k<4; k++){
Temp.tempValues[k] = 0;
}
for(int i = 0; i < lines.length; i++){
lines[i] = new Cords();
lines[i].setValues(Temp.tempValues[0], Temp.tempValues[1], Temp.tempValues[2], Temp.tempValues[3]);
}
}
由于您正在迭代行
数组,请在调用当前元素上的方法之前对其进行初始化:
for (int i = 0; i < lines.length; i++) {
lines[i] = new Cords();
lines[i].setValues(...);
}
for(int i=0;i
您还必须将跳线设置为静态。公共静态void main(字符串[]args){
public static void main(String[] args){
Cords[] lines = new Cords[5];
for(int k = 0; k<4; k++){
Temp.tempValues[k] = 0;
}
for(int i = 0; i < lines.length; i++){
lines[i] = new Cords();
lines[i].setValues(Temp.tempValues[0], Temp.tempValues[1], Temp.tempValues[2], Temp.tempValues[3]);
}
}
跳线[]线=新跳线[5];
对于(int k=0;kpublicstaticvoidmain(String[]args){
跳线[]线=新跳线[5];
对于(int k=0;k一个类的可能副本不能在一个文件中有两个公共类。这是必需的,因为我将在线发布代码,并且只能从一个文件中读取。请尝试删除“public”从该cord类中,然后尝试您的示例一个可能的副本不能在一个文件中有两个公共类。这是必需的,因为我将在线发布代码,并且只能从一个文件中读取。尝试从该cord类中删除“public”,然后尝试您的示例抱歉,我在for循环中,我的错误。忘记在行中键入它[i] =new Cords()
i get error:无法访问solution类型的封闭实例。必须使用solution类型的封闭实例限定分配。(例如x.new A()其中x是Solution的一个实例,请显示您的所有代码。否则我们就无能为力。很多代码可能会分散注意力。如果Cords类是静态的,我还能生成对象吗?对不起,我是在for循环中,我的错误。忘记在行[I]=new Cords()处键入它
I get error:无法访问solution类型的封闭实例。必须使用solution类型的封闭实例限定分配。(例如x.new A()如果x是solution的实例,请显示您的所有代码。否则我们将无能为力。很多代码可能会分散注意力。如果Cords类是静态的,我还可以创建对象吗?没有solution类型的封闭实例可访问。必须使用solution类型的封闭实例限定分配。(例如,x.new A()其中x是Solutionatlines[i]=new Cords()
@JohnDoe您想说什么?没有可访问的solution类型的封闭实例。必须使用solution类型的封闭实例限定分配。(例如x.new A(),其中x是Solutionatlines[i]=new Cords()
@JohnDoe您想说什么?