简体   繁体   English

如何从另一个类读取数组与数组的副本? [正在加载文件]

[英]How to read copy the array with the array from another class? [In loading file]

I want to create a game and I need to read file from the notepad when I use my loadfile.java alone, it work very well. 我想创建一个游戏,当我单独使用loadfile.java时,需要从记事本中读取文件,它工作得很好。 Then, I would like to copy my data into datafile.java as it will be easier for me to do the fighting scene. 然后,我想将我的数据复制到datafile.java中,因为这样对我来说更容易进行战斗。 However, I can't copy the array in my loadfile.java to the datafile.java and I don't understand why. 但是,我无法将我的loadfile.java中的数组复制到datafile.java中,我也不明白为什么。

import javax.swing.*;
import java.io.*;
import java.util.Scanner;

public class loadfile 
{
   static String filename = "Save.txt";
   static int size = 4;
   static int s;
   static int[] number;
   static String[] line;

   private static void load() throws IOException
   {
      BufferedReader reader = new BufferedReader(new FileReader(filename));
      while (reader.readLine()!= null)
      {
         size++;
      }
      size -= 4;
      reader.close();

      line = new String[size];
      number = new int[size];
      BufferedReader reader2 = new BufferedReader(new FileReader(filename));

      for (int i = 0; i < size; i++)
      {
         line[i] = reader2.readLine();
      }

      reader2.close();

      for (int i = 4; i < size; i++)
      {
         number[i] = Integer.parseInt(line[i]);
      }

   }

   public static String[] getData()
   {
      return line;
   }   

   public static int[] getNumber()
   {
      s = size - 4;
      int[] num = new int[s];
      for (int i = 0; i < s; i++)
      {
         num[i] = number[i+4];
      }
      return num;
   }

   public static int getDataSize()
   {
      return size;
   }

   public static int getNumberSize()
   {
      return size - 4;
   }

This is my loadfile.java I use the file with 4 names and 9 * n int in the notepad as I want to check whether I have the character first before I read the file. 这是我的loadfile.java,我在记事本中使用具有4个名称和9 * n int的文件,因为我想在读取文件之前先检查是否具有字符。 However, before I can handle this problem, I got another problem that I can't copy the array into my datafile.java 但是,在解决这个问题之前,我遇到了另一个问题,即无法将数组复制到datafile.java中。

The datafile.java is separate with two constructor. datafile.java由两个构造函数分隔。 One is for Starting the game and one is for loading the data. 一种是用于启动游戏,另一种是用于加载数据。 The constructor with the (int num) is the problem I have. 我有(int num)的构造函数。 First, I would like to show the java first: 首先,我想先展示一下Java:

import java.util.Arrays;
import java.io.*;
public class datafile
{
   private static String[] data;
   private static int[] number;
   private static String[] name;
   private static int[] a, d, s;
   private static int[] hp, maxhp;
   private static int[] mp, maxmp;
   private static int[] lv, exp;

   public datafile()
   {
      initialization();
      name[0] = "Pet";
      a[0] = 100;
      d[0] = 100;
      s[0] = 100;
      hp[0] = 500;
      mp[0] = 500;
      maxhp[0] = 500;
      maxmp[0] = 500;
      exp[0] = 100;
      lv[0] = 1;    
   }

   public datafile(int num) throws IOException
   {
      initialization();
      loadfile l = new loadfile();
      for (int i = 0; i < l.getNumberSize(); i++)
      {
         number[i] = l.getNumber()[i];
      }
      for (int i = 0; i < l.getDataSize(); i++)
      {
         data[i] = l.getData()[i];
      }
      for(int i = 0; i < 4; i++)
      {
         name[i] = data[i];
      }

      for(int i = 0; i < 4; i++)
      {
         a[i] = number[1+(i*9)];
         d[i] = number[2+(i*9)];
         s[i] = number[3+(i*9)];
         hp[i] = number[4+(i*9)];
         mp[i] = number[5+(i*9)];
         maxhp[i] = number[6+(i*9)];
         maxmp[i] = number[7+(i*9)];
         lv[i] = number[8+(i*9)];
         exp[i] = number[9+(i*9)];
      }

   }   

   public static String getName(int n)
   {
      return name[n];
   }

   public static int getAttack(int n)
   {
      return a[n];
   }

   public static int getDefense(int n)
   {
      return d[n];
   }

   public void initialization()
   {
      name = new String[3];
      a = new int[3];
      d = new int[3];
      s = new int[3];
      hp = new int[3];
      mp = new int[3];
      maxhp = new int[3];   
      maxmp = new int[3];
      lv = new int[3];
      exp = new int[3];   
   }

   public static void main (String[] args) throws IOException

   {
      new datafile(1);
   }
}

When I run the program, the debugging state this line 当我运行程序时,调试状态为这一行

data[i] = l.getData()[i];

as an error 作为错误

I don't know what wrong with this line and I tried so many different ways to change the way the copy the method. 我不知道这行有什么问题,我尝试了许多不同的方法来更改复制方法的方式。 However, it didn't work 但是,它没有用

The error says this: 该错误说明:

Exception in thread "main" java.lang.NullPointerException
    at datafile.<init>(datafile.java:38)
    at datafile.main(datafile.java:92)

I hope you guys can help me with this problem because I don't want to fail with my first work 我希望你们能帮助我解决这个问题,因为我不想因为第一份工作而失败

in your datafile(int num) you call 在你的数据文件(int num)中

loadfile l = new loadfile();

but you never call the load() method on you loadfile 但是您永远不会在loadfile上调用load()方法

l.load();

Edit: my bad, I didn't see your initialization method, but regardless, I'm going to stick with my recommendation that you radically change your program design. 编辑:不好,我没有看到您的初始化方法,但是无论如何,我将坚持我的建议,即您从根本上更改程序设计。 Your code consists of a kludge -- you've got many strangely named static array variables as some kind of data repository, and this suggests that injecting a little object-oriented design could go a long way towards creating classes that are much easier to debug, maintain and enhance: 您的代码包含了很多麻烦-您已经将许多名称奇怪的静态数组变量作为某种数据存储库,这表明注入一些面向对象的设计可能会大大有助于创建易于调试的类,维护和增强:

First I recommend that you get rid of all of the parallel arrays and instead create a class, or likely classes, to hold the fields that need to be bound together and create an ArrayList of items of this class. 首先,我建议您摆脱所有并行数组,而创建一个或多个可能的类,以容纳需要绑定在一起的字段并创建此类的ArrayList。

For example 例如

public class Creature {
   private String name;
   private int attack;
   private int defense; 

   // constructors here

   // getters and setters...
}

And elsewhere: 在其他地方:

private List<Creature> creatureList = new ArrayList<>();

Note that the Creature class, the repository for some of your data, should not be calling or even have knowledge of the code that loads the data, but rather it should be the other way around. 请注意,Creature类(一些数据的存储库)不应调用甚至不了解加载数据的代码,而应该相反。 The class that loads data should create MyData objects that can then be placed within the myDataList ArrayList via its add(...) method. 加载数据的类应创建MyData对象,然后可以通过其add(...)方法add(...)其放置在myDataList ArrayList中。

As a side recommendation, to help us now and to help yourself in the future, please edit your code and change your variable names to conform with Java naming conventions: class names all start with an upper-case letter and method/variable names with a lower-case letter. 作为附带建议,为了对我们有帮助,并在以后为您提供帮助,请编辑代码并更改变量名以符合Java命名约定:类名均以大写字母开头,方法/变量名以小写字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM