在Java上运行时出现错误: “空指针异常”

huangapple go评论62阅读模式
英文:

Run Time Error on Java: "Null Pointer Exception"

问题

以下是您提供的文本的中文翻译:

我遇到了这个运行时错误,错误消息显示为“空指针异常”。我是Java的初学者,不明白这是什么意思。

错误消息如下:

>  异常线程 "main" java.lang.NullPointerException
>     	at MyLongArray.insert(MyLongArray.java:26)
>     	at MyLongArray.main(MyLongArray.java:69)
import java.util.*;

public class MyLongArray
{
    private long a[];
    private int nElems;
    
    public MyLongArray(int size)
    {
        long[] a = new long[size];
        nElems = a.length;
    }
    
    public int find(long searchKey) {
        int m =0;
        for(int i=0; i < nElems; i++)
            if(searchKey == a[i])
                m = i;
        return m;
    }
    
    public void insert(long value) {
        @SuppressWarnings("resource")
        Scanner  sc = new Scanner(System.in);
        System.out.print("在哪个索引位置插入? "); 
        int i = sc.nextInt();
        a[i] = value;
    }
    
    public long getElem(int index) {
        return a[index];
    }
    
    public boolean delete(long value) {
        long[] temp = new long[nElems];
        int f = 0;
        int o = 0;
        for(int i=0; i < nElems; i++)
        {
            if(value != a[i])
            {
                temp[o++] = a[i];
            }
            else
                f = 1;
        }
        
        for(int j=0; j < nElems; j++)
            a[j] = temp[j];
        
        for(int i=0; i < nElems; i++)
            System.out.print(a[i] + " ");
        
        if (f==1)
            return true;
        else
            return false;
    }
    
    public void display() 
    {
        for(int i =0; i < nElems; i++)
            System.out.print(a[i] + " ");
    }    
    
    public static void main(String[] args)
    {
        MyLongArray m = new MyLongArray(5);
        m.insert(5);
        m.find(21);
        m.getElem(2);
        m.delete(3);
        m.display();
    }
}

希望这个翻译对您有帮助。如果您需要进一步的信息或帮助,请随时告诉我。

英文:

I am getting this run time error which says there is a null pointer exception. I am just a beginner at Java and don't understand what this means.

Error message is as follows:

> Exception in thread "main" java.lang.NullPointerException
> at MyLongArray.insert(MyLongArray.java:26)
> at MyLongArray.main(MyLongArray.java:69)

import java.util.*;
public class MyLongArray
{
private long a[];
private int nElems;
public MyLongArray(int size)
{
long[] a = new long[size];
nElems = a.length;
}
public int find(long searchKey) {
int m =0;
for(int i=0; i &lt; nElems; i++)
if(searchKey == a[i])
m = i;
return m;
}
public void insert(long value) {
@SuppressWarnings(&quot;resource&quot;)
Scanner  sc = new Scanner(System.in);
System.out.print(&quot;At what index do you want to insert? &quot;); int i = sc.nextInt();
a[i] = value;
}
public long getElem(int index) {
return a[index];
}
public boolean delete(long value) {
long[] temp = new long[nElems];
int f = 0;
int o = 0;
for(int i=0; i &lt; nElems; i++)
{
if(value != a[i])
{
temp[o++] = a[i];
}
else
f = 1;
}
for(int j=0; j &lt; nElems; j++)
a[j] = temp[j];
for(int i=0; i &lt; nElems; i++)
System.out.print(a[i] + &quot; &quot;);
if (f==1)
return true;
else
return false;
}
public void display() 
{
for(int i =0; i &lt; nElems; i++)
System.out.print(a[i] + &quot; &quot;);
}	
public static void main(String[] args)
{
MyLongArray m = new MyLongArray(5);
m.insert(5);
m.find(21);
m.getElem(2);
m.delete(3);
m.display();
}
}

答案1

得分: 1

在构造函数 MyLongArray 中,使用 long[] a = new long[size],你声明了一个新的局部数组 a,而没有初始化你的类变量数组。之后在插入方法中,你试图在一个未初始化的数组 a 中设置元素。

请改用 a = new long[size],而不是 long[] a = new long[size]

英文:

In constructor MyLongArray with long[] a = new long[size], you are declaring new local array a, instead of initializing your class variable array. After that in the insert method, you are trying to set elements in a non-initialized array a.

Use a = new long[size] instead of long[] a = new long[size].

答案2

得分: 0

你需要使用 a = new long[size]this.a = new long[size] 来初始化实例字段,而不是在构造函数内部声明局部变量 a

英文:

You need to use a = new long[size] or this.a = new long[size] to initialize the instance field instead of declaring a local variable a inside the constructor.

huangapple
  • 本文由 发表于 2020年8月9日 03:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63319594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定