Blog logotrial and stderr

java

A 3 post collection


Linting uninitialized field dereference in Java

 •  Filed under java

Am I out of my mind for thinking that some linter or other should be able to recognize the following code as buggy?

package com.company;

public class Main {

    public String greeting = "hey";

    private Main anotherMain;

    public Main getAnotherMain() {
        return anotherMain;
    }

    public void setAnotherMain(Main main) {
        this.anotherMain = main;
    }

    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.getAnotherMain().greeting);
    }
}

I expect a linter to tell me that the unchecked dereference main.getAnotherMain().greeting could produce an NPE because anotherMain defaults to null.

I tried SpotBugs, NullAway, and the IntelliJ static checker, and none of them can catch this. Bug filed against SpotBugs. We'll see how it goes.

NPE

 •  Filed under java

Type safety without null safety is like a biohazard suit with an open face.

This is very typical Java code. It exhibits obnoxiously verbose typing, and yet can crash your application with NullPointerExceptions at runtime.

SuperDatabaseConnector superDatabaseConnector = SuperDatabaseConnector.getInstance();

getResultsFrom(superDatabaseConnector);

public static SomethingElseButMaybeNull getResultsFrom(SuperDatabaseConnector superDatabaseConnector) {
  return superDatabaseConnector.getResults();
}

Java is a bad language and you shouldn't use it.