简体   繁体   中英

Java + Libgdx: Gdx.app.log() null pointer error

I am learning the LibGDX engine in parallel to re-learning java, and have written a simple logging class that has one method with a string argument to be passed to the Gdx.app.log() . while this isn't needed I did so to practice importing and using custom methods and classes, as well as reducing the length of the line needed to send a message to the console. the method looks like so:

import com.badlogic.gdx.Gdx;

public class logging {
  public static final String tag="Console";
  //contains method for logging to the console during testing.
  public void log(String message){
    Gdx.app.log(tag, message);
  }
}

Then in the class I am using it in, it is imported properly, and a public logging 'con' is created. Up to this point everything seems to work fine because when I type con. in eclipse I get the log(message) as an autocomplete option, however when it is actually called for instance in a screen, under the show() method. when the program tries to step through that point i get a java.lang.NullPointerException which is confusing the hell out of me since everything is written properly. as an example of its use:

con.log("this is a test");

is the exact usage I attempt which seems fine in eclipse before runtime. Is there some simple idea I am failing to grasp here? or a quirk to the Gdx.app.log() ? Please no responses with "just use the Gdx.app.log() ; where you need to write to the log" as this is not the point of the exercise for me. thank you in advance for all the help!

If you are getting a NullPointerException in this line:

con.log("this is a test");

The only thing that can be null is con. You are probably defining it, but not actually creating it.

Logging con;

and not

Logging con = new Logging();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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