0

When I tried to compile the program, it compiled successfully. But when I executed the program, this is the output I recieved in command prompt

Exception in thread "main" java.lang.NullPointerException at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838) at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122) at java.base/java.lang.Float.parseFloat(Float.java:455) at grade.main(grade.java:9)

 import javax.swing.JOptionPane;

    class grade{ 
             public static void main(String[] args){


         String engmark=null;
         String mathmark=null;
         String sstmark=null;
         String scimark=null;
         String compmark=null;
         float sc=Float.parseFloat(scimark);
         float en=Float.parseFloat(engmark);
         float ss=Float.parseFloat(sstmark);
         float mt=Float.parseFloat(mathmark);
         float co=Float.parseFloat(compmark);

         float totmark= sc+en+ss+co+mt;


         String response;


          response= JOptionPane.showInputDialog("Enter the maximum marks of each subject : ");

                 if (response==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (response.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                          else

                        engmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in english :");

                                        if (engmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (engmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }
                                else           
                                 mathmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in mathematics :");
                                        if (mathmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (mathmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                                else                                    
                                scimark= JOptionPane.showInputDialog(null,"Enter the marks obtained in science :");
                                        if (scimark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (scimark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                                else                                    
                                sstmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in S.st. :");
                                        if (sstmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (sstmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                                else                                    
                                compmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in computer :");
                                        if (compmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (compmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }



                     else

                     JOptionPane.showMessageDialog(null,"Your total marks is :" + totmark);                          

}

}

1 Answers1

0

Your code blows up at float sc=Float.parseFloat(scimark); The reason being scimark is null. Make it something else and change the rest of the ones to not have a null as an argument for parseFloat.

spinyBabbler
  • 392
  • 5
  • 19