在NXT Brick上读取上传的文件

时间:2019-02-05 15:22:41

标签: java file lejos-nxj

我正在尝试读取已上传到NXT Bricks本地文件系统的txt文件的内容,但是当我尝试启动我的应用程序时,我收到了NullPointerException

public static String line(String coord) throws Exception {

据我所知,nxt无法识别文件的存在。我还尝试使用文件路径访问计算机上txt文件的副本,但这也返回了NullPointerException

BufferedReader br = new BufferedReader(new FileReader("Files\\test.txt"));

    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
          //  sb.append(System.lineSeparator());
            line = br.readLine();
        }
        String everything = sb.toString();

        coord = everything;
    } catch(Exception e) {
        System.out.println(e);
    }
    return coord;



  heres the whole error exception:

  java.lang.NullPointerException
at js.tinyvm.CodeUtilities.processMethod(CodeUtilities.java:388)
at js.tinyvm.CodeUtilities.processCode(CodeUtilities.java:736)
at js.tinyvm.MethodRecord.postProcessCode(MethodRecord.java:205)
at js.tinyvm.MethodRecord.postProcessCode(MethodRecord.java:143)
at js.tinyvm.ClassRecord.storeCode(ClassRecord.java:798)
at js.tinyvm.Binary.processCode(Binary.java:737)
at js.tinyvm.Binary.createFromClosureOf(Binary.java:326)
at js.tinyvm.TinyVMTool.link(TinyVMTool.java:97)
at js.tinyvm.TinyVMTool.link(TinyVMTool.java:48)
at lejos.pc.tools.NXJLink.start(NXJLink.java:134)
at lejos.pc.tools.NXJLink.run(NXJLink.java:101)
at lejos.pc.tools.NXJLink.start(NXJLink.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at lejos.pc.tools.ToolStarter.startTool(ToolStarter.java:31)
at lejos.pc.tools.NXJLink.main(NXJLink.java:28)

我希望我的NXT读取存储在其文件系统中的文件的内容,因此感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我使用以下代码将坐标检索为属性

    Properties p = new Properties();
    File f = new File("coords.txt");
    if(f.exists()) {
        FileInputStream in;
        try {
            in = new FileInputStream(f);
            p.load(in);
            in.close();
        } catch (Exception e) {
            System.err.println("Failed to load coordinates");
            System.err.println(e.getMessage());
            Button.waitForAnyPress();
            System.exit(0);
        }
    }else {
        System.out.println("No coordinates found");
        while(!Button.ESCAPE.isPressed())
        {Thread.yield();}
        System.exit(0);
    }