Skip to main content
added 18 characters in body
Source Link
Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

{ String month = null; switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition fromfor null check before every switch use.

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

{ String month = null; switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use.

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

{ String month = null; switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition for null check before every switch use.

added 37 characters in body
Source Link
Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

{ String month = null;  switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use.

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

 switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use.

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

{ String month = null;  switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use.

Post Reopened by Louis Wasserman, Prashant Bhate, Paul Bellora, jszobody, Vitus
copy edit
Source Link
Paul Bellora
  • 55.4k
  • 18
  • 135
  • 185

Why doesn't String switch statment does notstatement support a null case?

I am just wondering why the Java 7 switchswitch statement does not support nulla null case and instead throws NPE NullPointerException? see See the commented line below (example taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.htmlthe Java Tutorials article on switch  ):

 switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use.

Why String switch statment does not support null case?

I am just wondering why Java 7 switch statement does not support null case and instead throws NPE ? see commented line below (example taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html  )

 switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use

Why doesn't String switch statement support a null case?

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch):

 switch (month) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; //case null: default: monthNumber = 0; break; } return monthNumber; } 

This would have avoided an if condition from every switch use.

Post Closed as "Opinion-based" by asteri, Nir Alfasi, CommunityBot, John3136, Reimeus
Source Link
Prashant Bhate
  • 11.2k
  • 9
  • 51
  • 84
Loading