英文:
What's wrong with my code for the toString() method?
问题
请注意,我是您的中文翻译,以下为翻译好的内容:
请注意,我是一个完全的编程初学者,发现学习Java相当困难。我有一个学校任务,需要我编写一个toString()方法的代码,我已经盲目地按照我的老师在课堂上写/教的内容进行了编写,但由于某种原因,当我运行测试文件时,我的代码似乎无法正常工作?我已经在下面包含了我的代码。
我确实收到一个错误,上面写着:“空指针访问:变量coef在此位置只能为null”,但是当我改变了所有的coef[]变量时,我的代码就变得无意义,因为它只等于null。
非常感谢任何指导!
编辑1:感谢您的回复,我以为我需要在toString()方法内部初始化coef[]作为一个变量(大错误!!)。还要感谢您提供了空指针问题的链接 - 上面有一些非常详细的解释,现在我明白我对coeff[]
变量进行了取消引用。
问题a:我已经删除了我的第一行,但现在似乎我的代码在这一行上失败了 return coef[1]+ "x + " + coef[0];
问题b:好奇知道如果类是复数形式为什么是一个不好的迹象?
public class Polynomials {
public Double coefficient;
public Integer exponent;
private int[] coef;
public String toString() {
String[] coef = null;
if (exponent <= -1)
return "0";
else if (exponent == 0)
return "" + coef[0];
else if (exponent == 1)
return coef[1] + "x + " + coef[0];
String s = coef[exponent] + "x^" + exponent;
return s;
}
}
英文:
please note that I'm a complete beginner to programming and have found learning Java quite hard. I have a school task that requires me to write a code for a toString() method and I've blindly followed what my teacher has been writing/teaching in class but for some reason my code doesn't seem to work when I run the test file? I've included my code below.
I do get an error that says the following- "Null pointer access: The variable coef can only be null at this location" but when I change all my coef[] variables, then my code becomes moot because it just equates to null.
Any pointers greatly appreciated!
EDIT 1: Thank you for your responses - I thought I needed to intialise coef[] as a variable inside my toString() method (big oops!!). Also thank you for providing the link to the NullPointer question - it had some really thorough explanations and now I understand that I dereferenced the coeff[]
variable.
QUERY a: I've removed my first line but now it seems that my code fails on this line return coef[1]+ "x + " + coef[0];
QUERY b: curious to know why it's a bad sign if the class is plural?
public class Polynomials {
public Double coefficient;
public Integer exponent;
private int[] coef;
public String toString() {
String[] coef = null;
if (exponent <= -1)
return "0";
else if (exponent == 0)
return "" + coef[0];
else if (exponent == 1)
return coef[1]+ "x + " + coef[0];
String s = coef[exponent] + "x^" + exponent;
return s;
}
答案1
得分: 2
你的Polynomials
类有3个字段,其中一个称为coef
。在你的toString
方法中,你声明了一个同样叫做coef
的局部变量。
在Java中,这意味着局部变量“遮蔽”了字段 - 换句话说,在整个方法中,coef
指的是String[] coef = null;
,而不是int[] coef
。
而且这个局部变量始终是空的 - 你创建它,将它初始化为空,然后从未改变过。因此,coef[0]
在运行时将保证抛出NullPointerException
。
修复方法似乎是... 只需完全删除那一行String[] coef = null;
。我不知道你为什么写那行代码或者它想要实现什么。
注:这个类的名字不应该是Polynomial
吗?用复数形式命名类通常不是一个好兆头。
英文:
Your Polynomials
class has 3 fields, and one is called coef
. In your toString method you then declare a local variable, also called coef
.
In java, that means the local variable 'shadows' the field - in other words, for that entire method, coef
refers to the String[] coef = null;
and not the int[] coef
.
And that local field is always null - you create it, initialize it to null, and never change it. Thus, choef[0]
will guaranteed throw a NullPointerException
at runtime.
The fix seems to be to .... just remove that String[] coef = null;
line entirely. I have no idea why you wrote that or what it's trying to accomplish.
NB: Shouldn't the class be named Polynomial
? Naming a class a plural is usually a bad sign.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论