Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 811029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:58:43+00:00 2026-05-15T00:58:43+00:00

I was experimenting with enum , and I found that the following compiles and

  • 0

I was experimenting with enum, and I found that the following compiles and runs fine on Eclipse (Build id: 20090920-1017, not sure exact compiler version):

public class SwitchingOnAnull {
    enum X { ,; }
    public static void main(String[] args) {
        X x = null;
        switch(x) {
            default: System.out.println("Hello world!");
        }
    }
}

When compiled and run with Eclipse, this prints "Hello world!" and exits normally.

With the javac compiler, this throws a NullPointerException as expected.

So is there a bug in Eclipse Java compiler?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:58:43+00:00Added an answer on May 15, 2026 at 12:58 am

    This is a bug. Here’s the specified behavior for a switch statement according to the Java Language Specification, 3rd Edition:

    JLS 14.11 The switch Statement

    SwitchStatement:
        switch ( Expression ) SwitchBlock
    

    When the switch statement is executed, first the Expression is evaluated. If the Expression evaluates to null, a NullPointerException is thrown and the entire switch statement completes abruptly for that reason.

    Apparently the bug in Eclipse has nothing to do with default case or enum at all.

    public class SwitchingOnAnull {
        public static void main(String[] args) {        
            java.math.RoundingMode x = null;
            switch(x) {};
    
            switch((Integer) null) {};
    
            switch((Character) null) {
                default: System.out.println("I've got sunshine!");
            }       
        }
    }
    

    The above code compiles and runs “fine” on (at least some version of) Eclipse. Each individual switch throws a NullPointerException when compiled with javac, which is exactly as the specification mandates.


    The cause

    Here’s javap -c SwitchingOnAnull when compiled under Eclipse:

    Compiled from "SwitchingOnAnull.java"
    public class SwitchingOnAnull extends java.lang.Object{
    public SwitchingOnAnull();
    Code:
     0: aload_0
     1: invokespecial  #8; //Method java/lang/Object."<init>":()V
     4: return
    
    public static void main(java.lang.String[]);
    Code:
     0: aconst_null
     1: astore_1
     2: getstatic     #16; //Field java/lang/System.out:Ljava/io/PrintStream;
     5: ldc           #22; //String I've got sunshine!
     7: invokevirtual #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    10: return
    
    }
    

    It seems that the Eclipse compiler gets rid of the entire switch constructs entirely. Unfortunately this optimization breaks the language specification.


    The official words

    The bug has been filed and assigned for fix.

    Olivier Thomann 2010-05-28 08:37:21 EDT

    We are too aggressive on the optimization.

    For:

      switch((Integer) null) {};
    

    we optimize out the whole switch statement when we should at least evaluate the
    expression.

    I’ll take a look.

    Candidate for 3.6.1.

    See also

    • Bug 314830 – Switching on a null expression doesn’t always throw NullPointerException
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just begun experimenting with nested masters in MVC2. I'm not quite sure why
I experimenting with jQuery. As I was trying I found out that I can't
In experimenting with this question I created an example that I utterly do not
While experimenting with this question on collections in Spring.NET , I discovered that Spring
I'm currently experimenting with build script, and since I have an ASP.net Web Part
I'm experimenting with internationalization by making a Hello World program that uses properties files
I am doing some experimenting (so the code below has lots of stuff that
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I'm experimenting with Linq and am having trouble figuring out grouping. I've gone through
I'm experimenting with VS 2008 Express edition and when I hit f1 on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.