gpt4 book ai didi

java - 处理传递 String 变量时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 16:35:54 24 4
gpt4 key购买 nike

我正在尝试编写一个处理 Excel 工作表的愚蠢版本的项目。我允许用户执行的命令之一是将值以 = 的格式分配给特定单元格。即 A3 = 72.3%。将值分配给单元格后,它就会打印出包含更新更改的整个电子表格。

我创建了不同的类来表示不同类型的细胞。值单元格是包含任何 double 值的单元格,百分比单元格是包含任何百分比值的单元格,文本单元格是包含文本的单元格。

当我尝试运行我的程序时,我尝试为单元格分配百分比值。 (A2 = 7.25%)当程序使用新分配的 A2 百分比单元格打印出整个电子表格时,它应该截断百分比中的小数并仅显示 7%。但是,我不断收到此错误消息

Exception in thread "main" java.lang.NullPointerException
at PercentCell.abbreviatedCellText(PercentCell.java:18)
at Spreadsheet.getGridText(Spreadsheet.java:127)
at Spreadsheet.processCommand(Spreadsheet.java:73)
at TextExcel.main(TextExcel.java:20)

这是我的代码的一部分,根据用户输入的内容分配特定的单元格类型:

//assignment command   
} else if (command.contains("=")) {
int eqIndex = command.indexOf("=");
if (!command.substring(eqIndex - 1, eqIndex).equals(" ") || !command.substring(eqIndex + 1, eqIndex + 2).equals(" ")) {
return "Formatting Error: please include a space before and after the ="; //error message
} else {
String[] input = command.split(" ", 3);
SpreadsheetLocation cell = new SpreadsheetLocation(input[0]);
int col = cell.getCol();
int row = cell.getRow();
if (col > COLUMNS || row > ROWS) {
return "Error: cell outside of spreadsheet"; //error message
}
//assigning a textcell
if (input[2].contains("\"")) {
String inputValue = input[2].substring(1, input[2].length() - 1);
TextCell textCell = new TextCell(inputValue);
spreadsheet[row][col] = textCell;
//assigning a percent cell
} else if (input[2].contains("%")) {
PercentCell percentCell = new PercentCell(input[2]);
spreadsheet[row][col] = percentCell;

单元格扩展父类(super class)、真实单元格的百分比:

public class RealCell implements Cell {
private String fullText;

public RealCell(String input) {
this.fullText = input;
}

//method that returns the display of the abbreviated cell
public String abbreviatedCellText() {
if (fullText.length() > 10) {
return fullText.substring(0, CELLWIDTH);
} else {
String output = fullText;
while(output.length() < CELLWIDTH) {
output += " ";
}
return output;
}
}

//method that returns the actual value in a real cell
public String fullCellText() {
return fullText;
}

//method that parses the user input into a double
public double getDoubleValue() {
return Double.parseDouble(fullText);
}
}

现在这是我的代码的问题部分,Percent Cell 类:

public class PercentCell extends RealCell {
private String fullText;

public PercentCell(String input) {
super(input);
}

//method that returns the display of the abbreviated cell, truncating the decimal
public String abbreviatedCellText() {
String value = fullText;
if (value.contains(".")) {
int decIndex = fullText.indexOf(".");
value = value.substring(0, decIndex) + "%";
}
if (value.length() > 10) {
return value.substring(0, CELLWIDTH);
} else {
String output = value;
while(output.length() < CELLWIDTH) {
output += " ";
}
return output;
}
}

//method that parses the user input into a double and returns the percent value into a decimal
public double getDoubleValue() {
double decimal = Double.parseDouble(fullText.substring(0, fullText.length() - 1));
return decimal/100;
}
}

如何修复此错误?如果需要对我的代码进行任何说明(因为这不是我的整个项目),请告诉我!

最佳答案

     public class PercentCell extends RealCell {

public PercentCell(String input) {
super(input);
}

//method that returns the display of the abbreviated cell, truncating the decimal
public String abbreviatedCellText() {
String value = super.fullCellText();
if (value.contains(".")) {
int decIndex = value.indexOf(".");
value = value.substring(0, decIndex) + "%";
}
if (value.length() > 10) {
return value.substring(0, CELLWIDTH);
} else {
String output = value;
while(output.length() < CELLWIDTH) {
output += " ";
}
return output;
}
}

您使用了 fullText 字段但没有给出值,并且 fullText 值始终为 null我认为这就是问题所在,但如果有帮助,请告诉我!

关于java - 处理传递 String 变量时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61948136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com