oop—在java中,在子类的函数中使用字符串会得到null

vlju58qv  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(313)

这个问题不太可能帮助任何未来的游客;它只与一个小的地理区域、一个特定的时刻或一个非常狭窄的情况有关,而这些情况通常不适用于互联网的全球受众。有关使此问题更广泛适用的帮助,请访问帮助中心。
7年前关门了。
我最近一直在学习java,它和大多数其他语言都有很大的不同。我和其他一些人站在那里搔首弄姿,不明白为什么有些东西不起作用,这是一个我都明白的问题。这个。时间。这是令人沮丧和恼火的,希望我能给其他人一些启示。
问过你自己吗
“我已经试了好几个小时想让它工作,但我似乎看不到它。我试图将字符串“reducerfractions”传递给fractionreducer类,但不知为什么,当它进入该类时,它会变成null?
我对java非常陌生,这只是我的第二个java程序,我不太明白为什么它不起作用。。。

父类:

public class FractionCalc extends FractionUI
{
//member variables for this class initialized here, all are kept within this class
protected String reducerFractions;
{
/*Paramiters in here math stuff ect*/
finalNum = (int)fin_num;
finalDem = (int)fin_dem;
reducerFractions = finalNum + "/" + finalDem; 
//puts these together in a protected string

System.out.println(reducerFractions); //<- testing here shows its working as needed
reducer.setFraction(finalNum, finalDem);
//overloading the function shows it's working for the two ints as ints themselves
reducer.setFraction(reducerFractions);
int rNum = reducer.getResultsNum();
int rDenom = reducer.getResultsDenom();
String debug = rNum + " " + rDenom;
System.out.println(debug); 
//right here gives back the correct numbers using an int approach
}

儿童班:

public class FractionReducer extends FractionCalc
{
  public void setFraction(String a)
{
    System.out.println(reducerFractions);
    //Right here it's saying it's null, I don't understand why...
    String[] stringReducedFraction = reducerFractions.split("/");
    double numerator = Float.valueOf(stringReducedFraction[0]);
    double denominator = Float.valueOf(stringReducedFraction[1]);
    //split up the string here for other uses
 }
//Other stuff
}

终端输出

2/1 //The string prints out fine in the parent class
null//but null in the child?
Exception in thread "main" java.lang.NullPointerException
    at FractionReducer.setFraction(FractionReducer.java:25)
    at FractionCalc.FractionCalc(FractionCalc.java:73)
    at FractionUI.Run(FractionUI.java:47)
    at MainP2.main(MainP2.java:19)

这里有一个明显的(对大多数人来说)问题,但只有当你经常使用对象时,这个问题才明显。

laximzn5

laximzn51#

你试图进入你的领域 reducerFractions 从从初始值设定项调用的方法。我认为在调用该方法时字段没有完全初始化。
你能贴出符合sscce标准的代码吗?

xtfmy6hx

xtfmy6hx2#

哦,天哪。问题显然出在共享权限上。
对的!
大多数人会认为你只需要把字符串交换给公众,好吧,我们不想这样做,我们需要保护我们的数据,密切保护它,以防止错误,错误的错误和错误的信息。
这里需要意识到的是,你需要从孩子的课堂上看到它。当然子类可以看到它,因为它是受保护的,但它也是动态的。所以它本质上在改变。
是的,所以我们需要做的就是把它改成静态的,这样信息就可以流动了,而且因为它受到保护,所以不会受到干扰。
我希望这能帮助你们中的一些人,或者至少有人。

protected static String reducerFractions;
bkkx9g8r

bkkx9g8r3#

从这里我可以看到你把reducefractions作为字符串参数a传递给fractionreducer。所以,如果你用a的引用来代替reducefraction的引用,它应该对你有用。这样地:

public void setFraction(String a)
{
    System.out.println(a);
    //Right here it's saying it's null, I don't understand why...
    String[] stringReducedFraction = a.split("/");
    double numerator = Float.valueOf(stringReducedFraction[0]);
    double denominator = Float.valueOf(stringReducedFraction[1]);
    //split up the string here for other uses
 }

使字符串受到保护并保持静态并不是真正可行的方法。如果你在fractionreducer中需要它,你应该把它传过来。

相关问题