NullPointerException

Here developers can talk about how to write a Parser for LogMX

Moderator: admin

Post Reply
mrgs123
Posts: 1
Joined: Tue Jun 30, 2015 4:06 pm

NullPointerException

Post by mrgs123 »

Hi, I run your sample parser up in eclipse, after adding a junit wrapper, and I call it thus:
@Test
public void testParseLine() {
String line1 = "07/03/2007, 16:51:00 (T0+1834ms) | ERROR | com.company.soft.DatabaseManager | A simple text on a single line";
String line2 = "07/03/2007, 16:51:01 (T0+2805ms) | INFO | com.company.soft.gui.MainFrame | A simple text on";
String line3 = "two lines";
String line4 = "7/03/2007, 16:51:02 (T0+3810ms) | WARNING | com.company.soft.gui.MainFrame | A simple text on a single line";

SampleParser P = new SampleParser();
try {
P.parseLine(line1);
P.parseLine(line2);
P.parseLine(line3);
P.parseLine(line4);
P.parseLine(null);
} catch (Exception e) {
fail("Exception thrown.");
}
}

and on line2, it throws a NullPointerException when it hits add_entry(entry); for the first time...

What am I doing wrong?

Cheers,
mrgs
admin
Site Admin
Posts: 555
Joined: Sun Dec 17, 2006 10:30 pm

Re: NullPointerException

Post by admin »

Hello,

This error occurs because the entries list for this Parser has not been created yet: Parsers need to be initialized before being used (that's what LogMX does). Because it is an internal mechanism, it is not very easy for you to create this list: the required method is obfuscated (i.e. renamed with a random method name like "a", "b", "dF", ... just before the release). For LogMX v5.3.4 eval version, this method is called "a()". You can recognize this method in any other version knowing that it has package-protected access, and takes a single "List" as parameter. So here is a way to make your code work:

Code: Select all

LogFileParser P = new SampleParser();
P.a(new ArrayList());
P.parseLine(line1);
P.parseLine(line2);
Notes: You need to declare P as a "LogFileParser" and not "SampleParser", and to bypass the issue of obfuscated code for the type of List, you can just create a raw type List (i.e. "ArrayList()" instead of "ArrayList<j>()" because "j" can change in future releases).

But maybe we should set this method as public without any obfuscation, for people like you who want to do Unit Tests (which is a very good idea :wink: ). What do you think?

Xavier
MartinO
Posts: 1
Joined: Fri Aug 30, 2019 11:59 am

Re: NullPointerException

Post by MartinO »

+1 for me.

I have observed this problem too and would love if you could make the function public. Using Junit when writing custom parser simplifies things a lot.

BR;
-Martin
admin
Site Admin
Posts: 555
Joined: Sun Dec 17, 2006 10:30 pm

Re: NullPointerException

Post by admin »

Hello,

OK, since there is clearly a need, we will add it in the next version (v8.0) :D (probably a "init()" public method to prepare the Parser if not already initialized).

We will let you know when it's available!

Thanks for your feedback.

Xavier
admin
Site Admin
Posts: 555
Joined: Sun Dec 17, 2006 10:30 pm

Re: NullPointerException

Post by admin »

Hello,

As promised, LogMX v8.0.0 is now available for download, and includes the new method "LogFileParser.init()" :) : https://logmx.com/dev/api/com/lightysof ... tml#init--

Let me know if you have any questions!

Xavier
Post Reply