The real crash debugging course:The real crash evoiding cource:Add to import section:
import org.apache.log4j.Logger;
Add to class fields section:
public Logger log = Logger.getLogger(this.getClass());
for static use (a bit more recomended but you wont have chance to get class instance, instance usefull when you have multiple):
public static Logger log = Logger.getLogger(classname.class);
All exception have specific source (game core), all errors generated are RuntimeExceptions, in most cases it's moment of object creation, there is sample of code that allow you to evoid exception due invalid hullid string:
private static FleetMemberAPI _secure;
public boolean secure_createFleetMember(String s) {
String r = "";
try {
// dont call core methods directly, you will aggro devs
// call API instead...
[s]//_secure = new FleetMember(0, s, FleetMemberType.SHIP);[/s]
_secure = Global.getFactory().createFleetMember(FleetMemberType.SHIP,s);
} catch (Exception e) {
log.info(s + " is invalid variant");
r = r + "INVALID VARIANT DETECTED: " + s + "\n\t\t";
return false;
}
return true;
}
If code return true its safe to use current FleetMemberAPI _secure variable to fleet compilation, if not - you shoud cancel your actions, error will be logged into starfarer.log, you will easy find it by name of class that generated error.
Same code can be used to test anything, it resource heavy but ensure smooth gameplay.
Just surround problematic locations with try+catch and add some logic to rest of code.
Logic can be hard to implement in some locations, for this case you shoud use special "null" objects of every type:
If hull not found - we create hull "debug_hull" and proceed normal operations;
If fleet not found - we create fleet "debug_fleet" and proceed normal operations;
Also strongly adviced to place lots of:
log.info("USER ACTIONS WAS" + S);
everywhere in code, this will help a lot in case of unexpected crush
This code can be used to display stacktrace ingame in case of expected error:
StackTraceElement[] stack = e.getStackTrace();
String exception = "";
for (StackTraceElement s : stack) {
exception = exception + s.toString() + "\n\t\t";
}
log.info(exception);