iv been working on a fairly simple part of my program but it throws a null pointer exception. The error i recieve is :

Exception in thread "main" java.lang.NullPointerException
    at FillStyle.<init>(FillStyle.java:12)
    at compiler.main(Builder.java:43

The code causing the error is :

    public FillStyle(int type)
    if (type >= 0) {
            UI8 g = new UI8(type);
            toutput = toutput.concat(g.ToUI8()); //Line 12
            //output is a string that adds a string another string from class UI8

The method is being called in main like this :

StyleArray ff = new StyleArray(new FillStyle(0));

Reasonably simple, and there is no syntax errors, but it seems to be that UI8 seems to be garbage collected or not being called correctly within the If condition causing it to be null pointer? The class UI8 just converts the int to a string, and the getOutput just retrieves the string.

toutput = toutput.concat(g.ToUI8());

here you have several options:
1. touptut is null
2. g is null
3. the result returned by g.ToUI8() is null

well, make sure that you validate for all these options, and, if possible, you provide a default value, or you instantiate the non initialised object right there.

is null

Thanks, it was the toutput that wasnt initialized in the constructor, woops rookie mistake.

This question has already been answered. Start a new discussion instead.