-4
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ stackoverflow.com/questions/25694872 \$\endgroup\$ Commented Feb 17, 2017 at 6:15
  • 5
    \$\begingroup\$ We strongly discourage single-language challenges. \$\endgroup\$ Commented Feb 17, 2017 at 11:54

2 Answers 2

7
\$\begingroup\$

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.
\$\endgroup\$
4
  • 1
    \$\begingroup\$ That is completely missing the point of the meta question and answer you linked to, which is about full program vs function. See meta.codegolf.stackexchange.com/a/2423/194 \$\endgroup\$ Commented Feb 17, 2017 at 6:52
  • \$\begingroup\$ Yes. You can save a byte by using enum instead of class. \$\endgroup\$ Commented Feb 17, 2017 at 8:21
  • \$\begingroup\$ @PeterTaylor could not get that to compile, but found a useful tip about Interface having default public scopes in Java 8. \$\endgroup\$ Commented Feb 17, 2017 at 8:36
  • \$\begingroup\$ It seems to need the declaration statement to be closed with a semicolon ideone which is still 56, what did I miss? \$\endgroup\$ Commented Feb 17, 2017 at 9:01
4
\$\begingroup\$

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.

\$\endgroup\$
11
  • \$\begingroup\$ 19 bytes, see this Meta post \$\endgroup\$ Commented Feb 17, 2017 at 6:35
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 17, 2017 at 6:39
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Feb 17, 2017 at 6:39
  • \$\begingroup\$ ((C)null).wait(); will work too. \$\endgroup\$ Commented Feb 17, 2017 at 6:47
  • \$\begingroup\$ wait's declaration throws InterruptedException, so the class will not compiler without a try/catch or throws declaration. \$\endgroup\$ Commented Feb 17, 2017 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.