the applet compiles fine(no errors) but while running when i press the accountButton(only button in the applet) i get errors, what is wrong with my applet ?
thanks in advance

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at AppletMetodos.actionPerformed(AppletMetodos.java:96)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

this is my code

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// <applet width="200" height="200" code="AppletMetodos"></applet>

public class AppletMetodos extends Applet implements ActionListener {
    Label accountNumberLabel, name, balanceLabel, result;
    TextField accountNumberField, nameField, balanceField;
    Button accountButton;
    TextArea resultArea;
    Panel infoPanel, display, buttonPanel;
    int MAX_ACCOUNTS = 100;
    int counter = 0;
    account user[];

    public void init() {
        // setBackground(Color.WHITE);
        setLayout(new BorderLayout());
        buttonPanel = new Panel(new FlowLayout());
        infoPanel = new Panel(new GridLayout(6, 1));
        display = new Panel(new BorderLayout());
        accountNumberLabel = new Label("Numero de Cuenta");
        accountNumberField = new TextField();
        name = new Label("Nombre Cliente");
        nameField = new TextField();
        balanceLabel = new Label("Saldo de Cuenta");
        balanceField = new TextField();
        accountButton = new Button("Despliega Cuenta");
        result = new Label("Resultado");
        resultArea = new TextArea("", 5, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
        infoPanel.add(accountNumberLabel);
        infoPanel.add(accountNumberField);
        infoPanel.add(name);
        infoPanel.add(nameField);
        infoPanel.add(balanceLabel);
        infoPanel.add(balanceField);
        buttonPanel.add(accountButton);
        display.add(result, BorderLayout.WEST);
        display.add(resultArea, BorderLayout.EAST);
        add(infoPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);
        add(display, BorderLayout.SOUTH);
        // accountNumberField.addActionListener(this);
        // nameField.addActionListener(this);
        // balanceField.addActionListener(this);
        accountButton.addActionListener(this);

    }

    class account {
        private int accountNumber;
        private String userName;
        private int accountBalance;

        account() {

        }

        account(int number, String name, int balance) {
            accountNumber = number;
            userName = name;
            accountBalance = balance;
        }

        void setAccount(int number, String name, int balance) {
            accountNumber = number;
            userName = name;
            accountBalance = balance;
        }

        void changeBalance(int newBalance) {
            accountBalance = newBalance;
        }

        void showResult() {
            resultArea.setText("" + resultArea.getText() + "/n" + accountNumber
                    + " " + userName + " $" + accountBalance);
        }
    }

    public void actionPerformed(ActionEvent ae) {

        int accountNumber = Integer.parseInt(accountNumberField.getText());
        String name = nameField.getText();
        int balance = Integer.parseInt(balanceField.getText());
        if (ae.getSource() == accountButton) {      // the problem should be here
            user[counter] = new account(accountNumber, name, balance);
            user[counter].showResult();
            counter++;
        }

    }
}

java.lang.NullPointerException
at AppletMetodos.actionPerformed(AppletMetodos.java:96)

One of the values on line 96 is null (ie not initialised). Print all the variables used on that line to see which one is null, then you can back-track to find out why. The array "user" would be the prime suspect...

commented: thanks for the help +0

thanks just solved it

user[counter] = new account(accountNumber, name, balance);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.