Menu

#19 File access throws NullPointerException

none
open
nobody
None
1
2016-05-26
2013-04-07
Rivenris
No

When I am trying to access data files I am getting Nulls as a result of getDataFile(...) and getDataDirectory().
My robot is being developed in external IDE, and it's classpath is added correctly (code without data access is running properly).

Discussion

  •  Rivenris

    Rivenris - 2013-04-07

    The question is how can I make data writing working?

     
  • Flemming N. Larsen

    Have a look at the source file for the SittingDuck sample robot. It gives a simple example of how to read and write to a file.
    Also make sure you have added your class path in Robocode to where your robots a located.
    Options -> Preferences -> Development Options

     
  •  Rivenris

    Rivenris - 2013-04-07

    Thank you for such quick response.
    I did try code from SittingDuck example, and it ended with the same error. Also I have already defined class path properly as I was able to run my robots properly without using File access.

    Here is example stack trace from my error (Learner is my robot name):

    java.lang.NullPointerException
    at net.sf.robocode.host.io.RobotFileSystemManager.getDataFile(RobotFileSystemManager.java:130)
    at net.sf.robocode.host.proxies.AdvancedRobotProxy$1.run(AdvancedRobotProxy.java:207)
    at net.sf.robocode.host.proxies.AdvancedRobotProxy$1.run(AdvancedRobotProxy.java:205)
    at java.security.AccessController.doPrivileged(Native Method)
    at net.sf.robocode.host.proxies.AdvancedRobotProxy.getDataFile(AdvancedRobotProxy.java:205)
    at robocode.AdvancedRobot.getDataFile(AdvancedRobot.java:707)
    at Learner.run(Learner.java:76)
    at net.sf.robocode.host.proxies.HostingRobotProxy.callUserCode(HostingRobotProxy.java:273)
    at net.sf.robocode.host.proxies.HostingRobotProxy.run(HostingRobotProxy.java:221)
    at java.lang.Thread.run(Unknown Source)

    Line 76 in Learner.java corresponds to the 43rd line from SittingDuck.java which is opening file for reading.

    // Read file "count.dat" which contains 2 lines, a round count, and a battle count
    reader = new BufferedReader(new FileReader(getDataFile("count.dat")));

    Similar error happens few lines later when robot tries to open file for printing.

     
  • Flemming N. Larsen

    Thanks for the stack trace. I have checked the code.
    The NullPointerException is thrown as Robocode cannot create and/or write to a data file for the robots.

    I need more details. Do you have write permissions to write to the files (especially the 'robot' directory) under the 'robocode' directory where Robocode is installed?
    Are you able to run SittingDuck without this error directly from Robocode? If not, something is wrong with your installation of Robocode or you have not got write permissions to the files. If it does work, your class path under the Development Options is not correct. Try to point to the 'robot' directory (containing all robots) - not directly on a individual robot directory.

     
  •  Rivenris

    Rivenris - 2013-04-07

    I am on windows running as admin, so it couldn't be lack of access. I run SittingDuck from within the robocode, so I started messing with my code. As it turned out it I forgot to put Lerner into package. After doing so problems disappeared, so I think my class path is ok after all.
    I guess robocode doesn't like default package robots, which isn't that bad as it forces good practices. However I think it would be good to show some warning 'bout that after loading such robot.
    Anyway thank you for assistance and good trails which led me to final conclusion.

     
  • Flemming N. Larsen

    You are welcome.

    I agree with you regarding getting no warnings for robots that have been created for the default package. Hence I created a bug report for it here:

    https://sourceforge.net/p/robocode/bugs/358/

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.