简体   繁体   中英

How to get the Jlist selected value

I am writing a tip calculator. You have to choose "service quality" which is gonna change the tip percentage.

I have tried Jlist.getSelectedValue() and I keep getting the same error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

jList1 = new javax.swing.JList<>();
String bill = jTextField1.getText();
        double realBill = Double.parseDouble(bill);
        String people = jTextField2.getText();
        int noOfPeople = Integer.parseInt(people);
        String selectQual= jList1.getSelectedValue();
        String[] strings = { "PERFECT 20%", "GOOD 15%", "NOT BAD 10%", "BAD 5%", "TRASH 0%" };
        int s = 0;
double servQual = 0;
        for (int i=0;i<4;i++){
            if (selectQual.equals(strings[i])){
                s = i;
                break;
            }
        }
        switch(s){
            case 0: servQual=0.2;
            break;
            case 1: servQual=0.15;
            break;
            case 2: servQual=0.1;
            break;
            case 3: servQual=0.05;
            break;
            case 4: servQual=0;
            break;
        }

        double finalCheck = (realBill*(1+servQual))/noOfPeople;
        DecimalFormat df = new DecimalFormat("#.##");
        JOptionPane.showMessageDialog(null,"Each person has to pay "+ df.format(finalCheck));
        JOptionPane.showMessageDialog(null,selectQual);

I expect to get the final bill per person in the dialogue box. However, the said error keeps showing up.

I am writing a tip calculator. You have to choose "service quality" which is gonna change the tip percentage.

I have tried Jlist.getSelectedValue() and I keep getting the same error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

jList1 = new javax.swing.JList<>();
String bill = jTextField1.getText();
        double realBill = Double.parseDouble(bill);
        String people = jTextField2.getText();
        int noOfPeople = Integer.parseInt(people);
        String selectQual= jList1.getSelectedValue();
        String[] strings = { "PERFECT 20%", "GOOD 15%", "NOT BAD 10%", "BAD 5%", "TRASH 0%" };
        int s = 0;
double servQual = 0;
        for (int i=0;i<4;i++){
            if (selectQual.equals(strings[i])){
                s = i;
                break;
            }
        }
        switch(s){
            case 0: servQual=0.2;
            break;
            case 1: servQual=0.15;
            break;
            case 2: servQual=0.1;
            break;
            case 3: servQual=0.05;
            break;
            case 4: servQual=0;
            break;
        }

        double finalCheck = (realBill*(1+servQual))/noOfPeople;
        DecimalFormat df = new DecimalFormat("#.##");
        JOptionPane.showMessageDialog(null,"Each person has to pay "+ df.format(finalCheck));
        JOptionPane.showMessageDialog(null,selectQual);

I expect to get the final bill per person in the dialogue box. However, the said error keeps showing up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM