-1

I want to convert JTextfield to int but java.lang.NullPointerException get numText through JTextfield. first convert to String and second, convert String into Integer and put that into other class.

package fitness_2;

import java.util.*;
import javax.swing.*;

import fitness_2.Person.Calculation;

import java.awt.event.*;


public class fitness extends JFrame {
    Calculation cal = null;
    public fitness()  {
        try {
         
        }catch(NumberFormatException e) {
        
        }catch(Exception e) {
        
        }
    
        JPanel panel = new JPanel();
        JLabel numOfPerson = new JLabel("number of members:");
        JTextField numText = new JTextField(4);
        JButton submitBtn = new JButton ("SUBMIT");
    
        panel.add(numOfPerson);
        panel.add(numText);
        panel.add(submitBtn);
        String n = numText.getText();
        submitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
    
        submitBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cal = new Calculation();
                if(!checkStr(n)) {
                    int num = Integer.parseInt(n);
                    cal.CalculationPane(num);    

                }
                
            }});
  
    
        add(panel);
        setVisible(true);
        setSize(600,400);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
        boolean checkStr(String s){
        boolean result = false;
        for(int i = 0 ;i < s.length();i++) {
            if(!Character.isDigit(s.charAt(i))) {
                result = false;
            }else
                result = true;
            }
        return result;
    }
    public static void main(String args[]) {
        new fitness();
    }
}

then, error is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at fitness_2.Person$Calculation.CalculationPane(Calculation.java:73)
at fitness_2.fitness$2.mouseClicked(fitness.java:44)
at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6639)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4556)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

and Calculation code

package fitness_2;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

import fitness_2.Person.Calculation;

import java.awt.event.*;

class Person {
    String name;
    int waitingTime;
    int satisfaction;
    int dissatisfaction;
    int index;

    Person () {
    }

    Person (String name, int waitingTime, int satisfaction, int 
dissatisfaction, int index) {
        this.name = name;
        this.waitingTime = waitingTime;
        this.satisfaction = satisfaction;
        this.dissatisfaction = dissatisfaction;
        this.index = index;
    }

static class Calculation implements MouseListener{
    JFrame frame;
    JPanel panel;
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;
    JTextField nameText, timeText, satisfactionText, dissatisfactionText;
    JButton calBtn;

    void CalculationPane(int n) {
        frame = new JFrame("Scheduling");
        panel = new JPanel();
        panel1 = new JPanel(new GridLayout(3,1));
        panel2 = new JPanel(new GridLayout(3,1));
        panel3 = new JPanel();
    
        Person p[] = new Person[n];
        for(int i = 0;i < n;i++) {
            JLabel nameLabel = new JLabel("Enter name: ", JLabel.CENTER);
            JLabel timeLabel = new JLabel("Enter waiting time: ",JLabel.CENTER);
            JLabel satisLabel = new JLabel("Enter satisfaction: ",JLabel.CENTER);
            JLabel dissatisLabel = new JLabel("Enter dissatisfaction: ",JLabel.CENTER);
        
            panel1.add(nameLabel);
            panel1.add(timeLabel);
            panel1.add(satisLabel);
            panel1.add(dissatisLabel);
        
            nameText = new JTextField(20);
            nameText.addMouseListener(this);
            timeText = new JTextField(3);
            timeText.addMouseListener(this);
            satisfactionText = new JTextField(4);
            satisfactionText.addMouseListener(this);
            dissatisfactionText = new JTextField(4);
            dissatisfactionText.addMouseListener(this);
        
            panel2.add(nameText);
            panel2.add(timeText);
            panel2.add(satisfactionText);
            panel2.add(dissatisfactionText);
        
        
            p[i].name = nameText.getText();
            p[i].waitingTime =Integer.parseInt(timeText.getText().trim());
            p[i].satisfaction = Integer.parseInt(satisfactionText.getText().trim());
            p[i].dissatisfaction = Integer.parseInt(dissatisfactionText.getText().trim());
            p[i].index = i;
        
        }
        calBtn = new JButton("Scheduling");
        calBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
    
        calBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                for (int i = 0; i < n - 1; i++) {
                    for (int j = 0; j < n - i - 1; j++) {
                        if ((double)p[j].dissatisfaction / ((double)p[j].waitingTime + 1) < (double)p[j + 1].dissatisfaction / ((double)p[j + 1].waitingTime + 1)) {
                            Person temp = new Person();
                            temp = p[j + 1];
                            p[j + 1] = p[j];
                            p[j] = temp;
                        }
                        else if ((double)p[j].dissatisfaction / (double)p[j].waitingTime == (double)p[j + 1].dissatisfaction / (double)p[j + 1].waitingTime) {
                            if (p[j].dissatisfaction > p[j + 1].dissatisfaction * p[j + 1].waitingTime)
                                continue;
                            else
                            {
                                Person temp = new Person();
                                temp = p[j + 1];
                                p[j + 1] = p[j];
                                p[j] = temp;
                            }
                        }
                        else
                            continue;
                    }
                }

                int tot_satisfaction = 0;
                System.out.println("Order of use: ");
                for (int i = 0; i < n; i++) {
                    System.out.println(p[i].index);
                    if (i < p[i].waitingTime) {
                        tot_satisfaction += p[i].satisfaction;
                    }
                    else
                        tot_satisfaction += p[i].satisfaction - ((i - p[i].waitingTime) * p[i].dissatisfaction);
            }
    System.out.println();
    System.out.println("Total satisfaction: " + tot_satisfaction);

            }
       });  
    }  
    @Override
    public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    @Override
    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    
    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    
    }
}
}

how can I fix them? I apply exception. but nothing was changed. Additionally, replacing "" to "0" either doesn't work.

1 Answers1

0

you need to add the getting of numText value inside button click, because the value will change each time and you need to get it everytime on click:

you defined the Calculation class inside Person class and its static, so to access the calculation, firstly you need to remove static from Calculation class or define the CalculationPane method as static to make it accessible from class, I removed the static keywork from Calculation

class Calculation {
}

and you need to initialize the Calculation class from Person class object (more details):

submitBtn.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        cal = new Person();
        String n = numText.getText();
        if (!checkStr(n)) {
            int num = Integer.parseInt(n);
            Person.Calculation c =  cal.new Calculation();
            c.CalculationPane(num);
        }
    }
});
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36