Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

java - Applet shows up blank, not initializing?

Hello everyone my name is Fyree! I have an assignment for an introductory Java course where I need to create a program that takes user input for the beginning values, and all five year values to make a line graph (but that is for another post), and also print the rate. Here is my problem, when I try to run this applet it opens (it compiles without error), but only shows up as a blank screen, and says applet not initialized at the bottom of the applet. I am not sure why this is (currently I am trying to get the rate part of the assignment working where it takes the year five value and beginning value given by the user, and uses them to print out the rate of growth or interest). I want to know why this is happening, and what I need to do to correctly have the rate output on the screen (or under the line graph once I have that put in). Any help would be appreciated as this assignment is due tomorrow!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rate_Graph extends JApplet implements ActionListener
{
JLabel bv, ev1, ev2, ev3, ev4, ev5;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5;
JButton go, add1, add2, add3, add4, add5;
public void init()
{
    setLayout(new FlowLayout()); //This sets the preferred layout format

    bv = new JLabel("Enter beginning value:"); //Beginning value input
    bv1 = new JTextField(5);

    ev1 = new JLabel("Enter year 1 value:"); //Year 1 value input
    ev_1 = new JTextField(5);

    ev2 = new JLabel("Enter year 2 value:"); //Year 2 value input
    ev_2 = new JTextField(5);

    ev3 = new JLabel("Enter year 3 value:"); //Year 3 value input
    ev_3 = new JTextField(5);

    ev4 = new JLabel("Enter year 4 value:"); //Year 4 value input
    ev_4 = new JTextField(5);

    ev5 = new JLabel("Enter year 5 value:"); //Year 5 value input
    ev_5 = new JTextField(5);

    go = new JButton("Add!"); //Button that implements all the values
    go.addActionListener(this); //So it does something
    add(bv); add(bv1); //Adding all of the different things
    add(ev1); add(ev_1);
    add(ev2); add(ev_2);
    add(ev3); add(ev_3);
    add(ev4); add(ev_4);
    add(ev5); add(ev_5);
    add(go);
}
public void actionPerformed(ActionEvent event)
{
        /*Object src = event.getSource(); 
        if(src==go){
            String strbv = bv1.getText();
            String strev1 = ev_1.getText();
            String strev2 = ev_2.getText();
            String strev3 = ev_3.getText();
            String strev4 = ev_4.getText();
            String strev5 = ev_5.getText();
        }*/
}
int intbv = Integer.parseInt(bv1.getText());
int intev1 = Integer.parseInt(ev_1.getText());
int intev2 = Integer.parseInt(ev_2.getText());
int intev3 = Integer.parseInt(ev_3.getText());
int intev4 = Integer.parseInt(ev_4.getText());
int intev5 = Integer.parseInt(ev_5.getText());
public double computeRate()
{
    double rate = (Math.pow(intev5 / intbv, 1.0 / 5.0) - 1);
    return rate;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your code you are getting NullPointerException because you are trying to initialize intbv, intbv1, ... by using JLabel's bv, bv1, .... which are not yet initialized.

Just only declare intbv, intbv1, intbv2...(not initialize), and assign them values when any action is performed in actionPerformed.

public void actionPerformed(ActionEvent event) {
    intbv = Integer.parseInt(bv1.getText());
    intev1 = Integer.parseInt(ev_1.getText());
    intev2 = Integer.parseInt(ev_2.getText());
    intev3 = Integer.parseInt(ev_3.getText());
    intev4 = Integer.parseInt(ev_4.getText());
    intev5 = Integer.parseInt(ev_5.getText());
    int sum = intbv + intev1 + intev2 + intev3 + intev4 + intev5;
    System.out.println("Sum            : " + sum);
    System.out.println("Compute rate   : " + computeRate());
}
int intbv;
int intev1;
int intev2;
int intev3;
int intev4;
int intev5;

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...