Results 1 to 2 of 2
  1. #1
    mjunit is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Angry Need HELP with java applet--- throws NullPointerException

    Hello im a beginner in java programming.

    When running this program i always receive an error saying "nullpointerexception String is null" any ideas how to fix it?

    The error is on line 68 (look for an arrow)


    import java.applet.Applet;
    import java.awt.*;
    public class Numbers extends Applet {
    TextField tf_x1, tf_x2, tf_y1, tf_y2;
    int drawX1, drawY1, drawX2, drawY2;
    public String x1_str, y1_str, x2_str, y2_str, slope_str, distanceOutput, slopeOutput, eqnofLineOutput, midpointOfLineOutput, yInterceptOutput, xInterceptOutput;
    double x1, x2, y1, y2, slope, FinalYIntercept, midPointX, midPointY;
    public String str_default = "0";
    public void init() {
    setSize(1000,600);
    Label label_1 = new Label("(x1, y1)");
    Label label_2 = new Label("(x2, y2)");
    tf_x1 = new TextField(str_default, 4);
    tf_y1 = new TextField(str_default, 4);
    tf_x2 = new TextField(str_default, 4);
    tf_y2 = new TextField(str_default, 4);
    add(label_1);
    add(tf_x1);
    add(tf_y1);
    add(label_2);
    add(tf_x2);
    add(tf_y2);
    add(new Button("Draw and Calculate"));
    add(new Button("Clear"));
    }
    private void getNewInput(){
    x1_str = tf_x1.getText();
    drawX1 = Integer.parseInt(x1_str);
    y1_str = tf_y1.getText();
    drawY1 = Integer.parseInt(y1_str);
    x2_str = tf_x2.getText();
    drawX2 = Integer.parseInt(x2_str);
    y2_str = tf_y2.getText();
    drawY2 = Integer.parseInt(y2_str);
    x1_str = tf_x1.getText();
    x1 = Double.parseDouble(x1_str);
    y1_str = tf_y1.getText();
    y1 = Double.parseDouble(y1_str);
    x2_str = tf_x2.getText();
    x2 = Double.parseDouble(x2_str);
    y2_str = tf_y2.getText();
    y2 = Double.parseDouble(y2_str);
    }
    public double FindingSlope(){
    return (y2 - y1) / (x2 - x1);
    }

    private void resetInputFields(){
    tf_x1.setText(str_default);
    tf_y1.setText(str_default);
    tf_x2.setText(str_default);
    tf_y2.setText(str_default);
    }
    private void resetCoordinates(){
    drawX1 = 0; drawY1 = 0; drawX2 = 0; drawY2 = 0;
    x1 = 0; y1 = 0; x2 = 0; y2 = 0;
    }
    public void paint(Graphics g){

    g.drawLine(350, 50, 350, 500);
    g.drawLine(600, 250, 100, 250);
    g.drawString("Slope:", 675, 175);
    g.drawString("Distance:", 675, 200);
    g.drawString("Midpoint Of Line:", 675, 225);
    g.drawString("Equation Of Line:", 675, 250);
    g.drawString("X Intercept:", 675, 275);

    g.drawString(slopeOutput, 750, 175); <=======HERE

    g.setColor(Color.blue);
    g.drawLine(drawX1 + 350, -drawY1 + 250, drawX2 + 350, -drawY2 + 250);
    }


    public boolean action(Event ev, Object arg){
    if(arg.equals("Draw and Calculate")){
    getNewInput();
    slope = FindingSlope();
    if (slope >= 0 || slope == 0 || slope <= 0){
    slopeOutput = (Double.toString (slope));
    }
    else{
    slopeOutput = ("Undefined");
    }



    repaint();
    }
    else if(arg.equals("Clear")){
    resetInputFields();
    resetCoordinates();
    repaint();
    }
    return true;
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    This probably had some parameters in an html file that initialized one or more of the fields.
    Try giving a value to the variable slopeOutput:
    Java Code:
        public String x1_str, y1_str, x2_str, y2_str,
                      slope_str, distanceOutput, [b][COLOR="Red"]slopeOutput = "1.0"[/COLOR][/b],
                      eqnofLineOutput, midpointOfLineOutput,
                      yInterceptOutput, xInterceptOutput;

Similar Threads

  1. Recursive Class throws NullPointerException
    By freeBatjko in forum New To Java
    Replies: 5
    Last Post: 11-03-2009, 10:18 AM
  2. Applet throws exception while recording
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 08-20-2009, 02:42 PM
  3. Replies: 1
    Last Post: 07-27-2009, 12:59 PM
  4. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:39 PM
  5. ERROR: nullPointerException in applet
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •