Shortest program to throw NullPointerException in Java

-4

I find NullPointerException to be interesting since there are no explicit pointers in Java. Here is a program that throws such an exception.

class c {
  public static void main(String[]s) {
    throw new NullPointerException();
  }
}

What is the shortest Java program that throws NullPointerException?

ceilingcat

Posted 2017-02-17T06:02:52.560

Reputation: 5 503

http://stackoverflow.com/questions/25694872/ – numbermaniac – 2017-02-17T06:15:21.277

5We strongly discourage single-language challenges. – Mego – 2017-02-17T11:54:53.487

Answers

3

62 Bytes (17 byte statement)

-2 bytes thanks to @PeterTaylor.

class C{public static void main(String[]s){s=null;s.clone();}}

Without knowing about the throw trick, this is the first thing that occurred to me. Using the main class's type is the shortest one available, and casting null is shorter than declaring C c=null;. notify is also the shortest method on an object that doesn't throw an exception.

mackthehobbit

Posted 2017-02-17T06:02:52.560

Reputation: 314

19 bytes, see this Meta post

– Jonathan Allan – 2017-02-17T06:35:12.893

@JonathanAllan doesn't the question ask for a "java program"? According to that meta post, the full program is required unless only a function is requested. – mackthehobbit – 2017-02-17T06:39:02.793

it was not specified as "full program" Meta (every Java program will have that same boilerplate anyway - so to compare them we can just count the content).

– Jonathan Allan – 2017-02-17T06:39:41.577

((C)null).wait(); will work too. – Jonathan Allan – 2017-02-17T06:47:38.407

wait's declaration throws InterruptedException, so the class will not compiler without a try/catch or throws declaration. – mackthehobbit – 2017-02-17T06:50:26.143

Compiles, runs and raises a NullPointerException at ideone.

– Jonathan Allan – 2017-02-17T06:53:17.000

You don't need a class name at all since you have a variable. s=null;s is one char shorter than ((C)null) – Peter Taylor – 2017-02-17T06:56:46.293

@JonathanAllan that only works with the added throws declaration on main. – mackthehobbit – 2017-02-17T07:00:21.353

@mackthehobbit Ah, yes. – Jonathan Allan – 2017-02-17T07:02:27.067

@PeterTaylor huh, that didn't occur to me at all. Nice! – mackthehobbit – 2017-02-17T07:02:32.103

clone() also requires a throws because of CloneNotSupportedException – Peter Taylor – 2017-02-17T08:21:07.977

5

Java 8 - 56 53 bytes (11 byte snippet)

interface P{static void main(String[]a){throw null;}}

ideone

From the documentation:

Class NullPointerException

...
Thrown when an application attempts to use null in a case where an object is required. These include:
...

  • Throwing null as if it were a Throwable value.

Jonathan Allan

Posted 2017-02-17T06:02:52.560

Reputation: 67 804

1

That is completely missing the point of the meta question and answer you linked to, which is about full program vs function. See http://meta.codegolf.stackexchange.com/a/2423/194

– Peter Taylor – 2017-02-17T06:52:31.720

Yes. You can save a byte by using enum instead of class. – Peter Taylor – 2017-02-17T08:21:41.363

@PeterTaylor could not get that to compile, but found a useful tip about Interface having default public scopes in Java 8.

– Jonathan Allan – 2017-02-17T08:36:49.517

It seems to need the declaration statement to be closed with a semicolon ideone which is still 56, what did I miss?

– Jonathan Allan – 2017-02-17T09:01:30.213