0

I am running a simple applet in my machine.Note that when executing applet "Number format exception" error occurs when the applet is trying to run. The below code is shown

import java.util.*;
import java.awt.*;
import java.applet.*;

<html>
<body>
<applet code="s09_03.class" width=400 height=400>
</applet>
</body>
</html>

public class s09_03 extends Applet
{
GregorianCalendar cal=new GregorianCalendar();
String s,s1,s2,s3,s4;
int a=0,b=0,c=0,d=0;
public void start(){s=getParameter("fg");
s1=getParameter("as");
s2=getParameter("as1");
s3=getParameter("as2");
s4=getParameter("as3");
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
d=Integer.parseInt(s4);
}
public void init()
{
}
public void paint(Graphics g)
{
if(s.equals("red"))g.setColor(Color.red);g.drawRect(a,b,c,d);
g.drawString("Color = "+"",25,25);
g.drawString("Calendar is"+cal.DATE+"/"+cal.MONTH+"/"+cal.YEAR,34,36);
}
}

The command used are

javac s09_03.java
and
appletviewer s09_03.java

Terminal output:

  java.lang.NumberFormatException: null 
    at java.lang.Integer.parseInt(Integer.java:443)
    at java.lang.Integer.parseInt(Integer.java:514)
    at s09_03.start(s09_03.java:22)
    at sun.applet.AppletPanel.run(AppletPanel.java:477)
    at java.lang.Thread.run(Thread.java:701).

So my question is why this error occurs and when does it usually occur??Also suggest some necessary changes in code so that the code runs without any error.Note that the code is being run through linux...Thanks...

~

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
user3046211
  • 466
  • 2
  • 13
  • Why it happens - your parameter (or parameters) aren't convertible to an `Integer`. How do you fix it? Always pass integral values to those parameters. – Makoto Apr 03 '14 at 05:19
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 04 '14 at 05:05

3 Answers3

0

Take a look Try-Catch, NumberFormatException, nullPointerException

Use try catch to handle the Exception

Try this, because you got the value as null. so check the passing value should not be null

        try
        {
        a = Integer.parseInt(s1 != null ? s1 : "0");
        b = Integer.parseInt(s2 != null ? s2 : "0");
        c = Integer.parseInt(s3 != null ? s3 : "0");
        d = Integer.parseInt(s4 != null ? s4 : "0");
        }
        catch(NumberFormatException ex)
        {
            System.out.println("Exception : "+ex);
        }

And also the variable s is also null so you got the NullPointerException

if (s != null && s.equals("red"))

instead of

if (s.equals("red"))
newuser
  • 8,338
  • 2
  • 25
  • 33
  • Hey i tried as u said..but the applet wouldnt run..When the code is executed it would simply just show appletviewer box but no contents in it..Why is it like this ???.Any suggestions to be made to code would be appreiated.. – user3046211 Apr 03 '14 at 05:41
  • hey the code ran successfully but as i have set the color to red in code using g.setColor(Color.red) red color is not painted...i not seeing that red color...Also it is showing wrong calendar....Any corrections to be made as u suugest it ?? – user3046211 Apr 03 '14 at 05:57
  • change the calendar values like cal.get(cal.DATE) + "/" + cal.get(cal.MONTH) + "/" + cal.get(cal.YEAR) because cal.DATE only returns the constant value, and so on for the month and year. Using the cal.get() to get the values of the date, month and year – newuser Apr 03 '14 at 06:03
  • Color red is not set for the components, because the s value is null, you assigned the red color while the condition is true, otherwise it wont set. – newuser Apr 03 '14 at 06:06
  • Hey thanks for your suggestion....it worked for me by using cal.get().Can i make Any improvements to the code ??.Is there any way to set color red???If yes how ?? – user3046211 Apr 03 '14 at 06:13
  • Everythings ok. For red color directly assign the red to String s = "red"; or dont check the condition directly set the color as red. – newuser Apr 03 '14 at 06:20
0

Catch the Exception and handle it

try{    
    a=Integer.parseInt(s1);
    b=Integer.parseInt(s2);
    c=Integer.parseInt(s3);
    d=Integer.parseInt(s4);
} catch(NumberFormatException e){
    //exceptionhandling
}
Markus
  • 1,649
  • 1
  • 22
  • 41
  • You need to handle the Exception there. Usually `e.printStackTrace()` to show what happened or your custom exception handling. The custom handling is use-case dependent – Markus Apr 03 '14 at 05:29
0

Simply catch the exception in catch block and take the appropriate action.

try{
  // some code that may throw NumberFormatException 

}catch(NumberFormatException e){
  // when number format exception occur, take appropriate action
}

Root cause of your problem:

As exception says clearly
  java.lang.NumberFormatException: null 
    at java.lang.Integer.parseInt(Integer.java:443)

You are trying to parse a String value that is null into an Integer, that is why parseInt() threw an exception.

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72