Java 未触及代码块
我正在用Java构建一个小应用程序,一个小游戏机制,但没什么大不了的。我有一个类,其目的是从文件中获取数据。但是当我声明这两个类从中读取时,程序只是忽略所有内容并继续。因此,当我尝试访问相应的列表时,它会给我空指针异常。获取以下数据的方法的代码:Java 未触及代码块,java,list,file,null,Java,List,File,Null,我正在用Java构建一个小应用程序,一个小游戏机制,但没什么大不了的。我有一个类,其目的是从文件中获取数据。但是当我声明这两个类从中读取时,程序只是忽略所有内容并继续。因此,当我尝试访问相应的列表时,它会给我空指针异常。获取以下数据的方法的代码: public void getData(int l, player tmp, level le) { String[] dataPlayer; String[] dataLevel; try {
public void getData(int l, player tmp, level le) {
String[] dataPlayer;
String[] dataLevel;
try {
//FileReader f = new FileReader(this.levelPath.concat(Integer.toString(l)));
File f = new File(this.levelPath.concat(Integer.toString(l)));
BufferedReader buff = new BufferedReader(new FileReader(f));
System.out.println("Reached");
boolean eof = false;
while (!eof) {
String b = buff.readLine();
if (b == null)
eof = true;
else {
if (b.contains("player")) {
dataPlayer = b.split("-");
for (int i = 0; i < dataPlayer.length; i++) {
if (i == 0)
continue;
items it = new items(dataPlayer[i]);
tmp.setInventory1(it);
}
}else if (b.contains("level")) {
dataLevel = b.split("-");
for (int i = 0; i < dataLevel.length; i++) {
if (i == 0)
continue;
items it = new items(dataLevel[i]);
le.setSpecific(it);
}
}
}
}
}catch (IOException i) {
i.getMessage();
}
}
这个特殊问题的问题是路径,它需要绝对值,比如/home/toomlg4u/IdeaProjects/javaProject/src/Data/levelData。您在try/catch中做了很多事情,可能不会引发IOException。如果你得到任何其他异常,它不会被捕获。这可能会导致奇怪的行为,具体取决于您有什么其他异常处理。对于调试,您可以捕获所有异常,并查看是否获得了其他内容。您在try/catch中做了很多事情,这些东西可能不会引发IOException。如果你得到任何其他异常,它不会被捕获。这可能会导致奇怪的行为,具体取决于您有什么其他异常处理。对于调试,您可以捕获所有异常,并查看是否有其他异常。这就是它在Java中看起来像一个体面的程序的方式:
private Stream<Items> asStreamOfItems(String line){
return Stream.of(line.split("-")).skip(1).map(Items::new);
}
public void parseFile(String pathToTheFile) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(pathToTheFile));
List<Items> players = lines.stream().filter(line -> line.contains("player")).flatMap(this::asStreamOfItems).collect(Collectors.toList());
List<Items> levels = lines.stream().filter(line -> line.contains("level")).flatMap(this::asStreamOfItems).collect(Collectors.toList());
........
}
私有流asStreamOfItems(字符串行){
返回Stream.of(line.split(“-”).skip(1).map(Items::new);
}
public void parseFile(字符串路径到文件)引发IOException{
列表行=Files.readAllLines(path.get(pathToTheFile));
List players=lines.stream().filter(line->line.contains(“player”)).flatMap(this::asStreamOfItems).collect(Collectors.toList());
List levels=lines.stream().filter(line->line.contains(“level”).flatMap(this::asStreamOfItems).collect(Collectors.toList());
........
}
在这种情况下,你所有奇怪的错误都会消失
在你编辑了这篇文章之后,我看到了你的文件内容。在这种情况下,代码应该如下所示:
class Items {
private final String name;
public Items(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Items parse(String line) {
return new Items(line.split("-")[1]);
}
}
public void parseFile(String pathToTheFile) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(pathToTheFile));
List<Items> players = lines.stream().filter(line -> line.contains("player")).map(Items::parse).collect(Collectors.toList());
List<Items> levels = lines.stream().filter(line -> line.contains("level")).map(Items::parse).collect(Collectors.toList());
..............
}
public void getData(int l, player tmp, level le) {
try (BufferedReader buff = new BufferedReader(new FileReader(new File(this.levelPath + l)))) {
String b;
while ((b = buff.readLine()) != null) {
if (b.contains("player")) {
String[] dataPlayer = b.split("-");
items it = new items(dataPlayer[1]); //because you know that you will have an array with only 2 elements
tmp.setInventory1(it);
}else if (b.contains("level")) {
String[] dataLevel = b.split("-");
items it = new items(dataLevel[1]); //because you know that you will have an array with only 2 elements
le.setSpecific(it);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
类项目{
私有最终字符串名;
公共项目(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共静态项解析(字符串行){
返回新项目(行拆分(“-”[1]);
}
}
public void parseFile(字符串路径到文件)引发IOException{
列表行=Files.readAllLines(path.get(pathToTheFile));
List players=lines.stream().filter(line->line.contains(“player”)).map(Items::parse).collect(Collectors.toList());
List levels=lines.stream().filter(line->line.contains(“level”).map(Items::parse).collect(Collectors.toList());
..............
}
顺便说一句,您打破了许多Java和通用编程规则,如:
tmp.setInventory1(it);
)非常糟糕的做法这就是它在Java中看起来像一个体面的程序的方式:
private Stream<Items> asStreamOfItems(String line){
return Stream.of(line.split("-")).skip(1).map(Items::new);
}
public void parseFile(String pathToTheFile) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(pathToTheFile));
List<Items> players = lines.stream().filter(line -> line.contains("player")).flatMap(this::asStreamOfItems).collect(Collectors.toList());
List<Items> levels = lines.stream().filter(line -> line.contains("level")).flatMap(this::asStreamOfItems).collect(Collectors.toList());
........
}
私有流asStreamOfItems(字符串行){
返回Stream.of(line.split(“-”).skip(1).map(Items::new);
}
public void parseFile(字符串路径到文件)引发IOException{
列表行=Files.readAllLines(path.get(pathToTheFile));
List players=lines.stream().filter(line->line.contains(“player”)).flatMap(this::asStreamOfItems).collect(Collectors.toList());
List levels=lines.stream().filter(line->line.contains(“level”).flatMap(this::asStreamOfItems).collect(Collectors.toList());
........
}
在这种情况下,你所有奇怪的错误都会消失
在你编辑了这篇文章之后,我看到了你的文件内容。在这种情况下,代码应该如下所示:
class Items {
private final String name;
public Items(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Items parse(String line) {
return new Items(line.split("-")[1]);
}
}
public void parseFile(String pathToTheFile) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(pathToTheFile));
List<Items> players = lines.stream().filter(line -> line.contains("player")).map(Items::parse).collect(Collectors.toList());
List<Items> levels = lines.stream().filter(line -> line.contains("level")).map(Items::parse).collect(Collectors.toList());
..............
}
public void getData(int l, player tmp, level le) {
try (BufferedReader buff = new BufferedReader(new FileReader(new File(this.levelPath + l)))) {
String b;
while ((b = buff.readLine()) != null) {
if (b.contains("player")) {
String[] dataPlayer = b.split("-");
items it = new items(dataPlayer[1]); //because you know that you will have an array with only 2 elements
tmp.setInventory1(it);
}else if (b.contains("level")) {
String[] dataLevel = b.split("-");
items it = new items(dataLevel[1]); //because you know that you will have an array with only 2 elements
le.setSpecific(it);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
类项目{
私有最终字符串名;
公共项目(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共静态项解析(字符串行){
返回新项目(行拆分(“-”[1]);
}
}
public void parseFile(字符串路径到文件)引发IOException{
列表行=Files.readAllLines(path.get(pathToTheFile));
List players=lines.stream().filter(line->line.contains(“player”)).map(Items::parse).collect(Collectors.toList());
List levels=lines.stream().filter(line->line.contains(“level”).map(Items::parse).collect(Collectors.toList());
..............
}
顺便说一句,您打破了许多Java和通用编程规则,如:
tmp.setInventory1(it);
)非常糟糕的做法如果希望保留循环代码,则可以重构代码,使其看起来像这样:
class Items {
private final String name;
public Items(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Items parse(String line) {
return new Items(line.split("-")[1]);
}
}
public void parseFile(String pathToTheFile) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(pathToTheFile));
List<Items> players = lines.stream().filter(line -> line.contains("player")).map(Items::parse).collect(Collectors.toList());
List<Items> levels = lines.stream().filter(line -> line.contains("level")).map(Items::parse).collect(Collectors.toList());
..............
}
public void getData(int l, player tmp, level le) {
try (BufferedReader buff = new BufferedReader(new FileReader(new File(this.levelPath + l)))) {
String b;
while ((b = buff.readLine()) != null) {
if (b.contains("player")) {
String[] dataPlayer = b.split("-");
items it = new items(dataPlayer[1]); //because you know that you will have an array with only 2 elements
tmp.setInventory1(it);
}else if (b.contains("level")) {
String[] dataLevel = b.split("-");
items it = new items(dataLevel[1]); //because you know that you will have an array with only 2 elements
le.setSpecific(it);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
它比您拥有的要好一点,更易于调试和阅读。我建议你读一下
根据经验,每次打开流时都必须关闭它。当你自己不打开它时,就不要关闭它。如果你想保持循环代码不变,那么你可以重构代码,使其看起来像这样:
class Items {
private final String name;
public Items(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Items parse(String line) {
return new Items(line.split("-")[1]);
}
}
public void parseFile(String pathToTheFile) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(pathToTheFile));
List<Items> players = lines.stream().filter(line -> line.contains("player")).map(Items::parse).collect(Collectors.toList());
List<Items> levels = lines.stream().filter(line -> line.contains("level")).map(Items::parse).collect(Collectors.toList());
..............
}
public void getData(int l, player tmp, level le) {
try (BufferedReader buff = new BufferedReader(new FileReader(new File(this.levelPath + l)))) {
String b;
while ((b = buff.readLine()) != null) {
if (b.contains("player")) {
String[] dataPlayer = b.split("-");
items it = new items(dataPlayer[1]); //because you know that you will have an array with only 2 elements
tmp.setInventory1(it);
}else if (b.contains("level")) {
String[] dataLevel = b.split("-");
items it = new items(dataLevel[1]); //because you know that you will have an array with only 2 elements
le.setSpecific(it);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
比那好一点