Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查时出现Java NumberFormatException_Java - Fatal编程技术网

检查时出现Java NumberFormatException

检查时出现Java NumberFormatException,java,Java,很抱歉,如果这有一个简单的解决方案,但我想不出来 我对NumberFormatException(错误)有问题: 我有一个按钮,当按下它时,它会检查不同的东西,但它不喜欢一行代码 行:if(Integer.parseInt(CarsLv1.cars[j])==0){ 我试图使这一行不同,但它仍然会产生相同的错误: if(j == 3){ Carslv1.cars=字符串数组。 所有其他类似CarMain=整数数组 完整代码,如果单击按钮: if(ae.getSource() == jbtnBu

很抱歉,如果这有一个简单的解决方案,但我想不出来

我对NumberFormatException(错误)有问题:

我有一个按钮,当按下它时,它会检查不同的东西,但它不喜欢一行代码

行:
if(Integer.parseInt(CarsLv1.cars[j])==0){

我试图使这一行不同,但它仍然会产生相同的错误:

if(j == 3){
Carslv1.cars=字符串数组。 所有其他类似CarMain=整数数组

完整代码,如果单击按钮:

if(ae.getSource() == jbtnBuildCarLv1){
    if(CarMain.main[5] >=1){
        if(CarMain.main[7] >0){
            if(CarFrame.frames[1] >0){ 

                for(int i=1;i <= 25;i+=2){
                    if(Part.parts[i] >0){
                        count0+=1;

                    }
                    else{
                        JOptionPane.showMessageDialog (null, "You don't have enought parts!", "No Parts", JOptionPane.ERROR_MESSAGE, ImagesHolder.NoPartsIcon);}                            
                }

            if (count0 == 13){              
                for (int j = 3;j <= 80; j+= 4){
                    if(Integer.parseInt(CarsLv1.cars[j]) == 0){                 
                        for(int i=1;i <= 25;i+=2){
                            Part.parts[i] --;

                            int count = Integer.parseInt(CarsLv1.cars[j]); count+=1; 
                            CarsLv1.cars[j] = Integer.toString(count);

                            if(j == 3){
                                JOptionPane.showMessageDialog (null, "You have gained Aston Martin DB9!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Aston_Martin_DB9Icon);
                            }
                            if(j == 7){
                                JOptionPane.showMessageDialog (null, "You have gained Acura NSX!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Acura_NSXIcon);
                            }
                            if(j == 11){
                                JOptionPane.showMessageDialog (null, "You have gained Ford Mustang!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Ford_MustangIcon);
                            }
                            if(j == 15){
                                JOptionPane.showMessageDialog (null, "You have gained Moskvich 412!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Moskvich_412Icon);
                            }
                            if(j == 19){
                                JOptionPane.showMessageDialog (null, "You have gained Kia Venga!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Kia_VengaIcon);
                            }
                            if(j == 23){
                                JOptionPane.showMessageDialog (null, "You have gained Fiat 1500!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Fiat_1500Icon);
                            }
                            if(j == 27){
                                JOptionPane.showMessageDialog (null, "You have gained Ferrari Enzo!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Ferrari_EnzoIcon);
                            }
                            if(j == 31){
                                JOptionPane.showMessageDialog (null, "You have gained Aston Martin Rapide!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Aston_Martin_RapideIcon);
                            }
                            if(j == 35){
                                JOptionPane.showMessageDialog (null, "You have gained Koenigsegg CCX!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Koenigsegg_CCXIcon);
                            }
                            if(j == 39){
                                JOptionPane.showMessageDialog (null, "You have gained Honda Civic!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Honda_CivicIcon);
                            }
                            if(j == 43){
                                JOptionPane.showMessageDialog (null, "You have gained Cadilliac ATS!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Cadilliac_ATSIcon);
                            }
                            if(j == 47){
                                JOptionPane.showMessageDialog (null, "You have gained Mitsubishi Lancer Evolution X!", "Congralations", JOptionPane.INFORMATION_MESSAGE,ImagesHolder.Mitsubishi_LancerEvolutionXIcon);
                            }
                            if(j == 51){
                                JOptionPane.showMessageDialog (null, "You have gained Infiniti FX!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Infiniti_FXIcon);
                            }
                            if(j == 55){
                                JOptionPane.showMessageDialog (null, "You have gained Lancia Thema!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Lancia_ThemaIcon);
                            }
                            if(j == 59){
                                JOptionPane.showMessageDialog (null, "You have gained Volvo XC90!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Volvo_XC90Icon);
                            }
                            if(j == 63){
                                JOptionPane.showMessageDialog (null, "You have gained Acura CSX!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Acura_CSXIcon);
                            }
                            if(j == 67){
                                JOptionPane.showMessageDialog (null, "You have gained Saab Sonett II!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Saab_SonettIIIcon);
                            }
                            if(j == 71){
                                JOptionPane.showMessageDialog (null, "You have gained Renault Floride!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Renault_FlorideIcon);
                            }
                            if(j == 75){
                                JOptionPane.showMessageDialog (null, "You have gained BMW e34!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.BMW_e34Icon);
                            }
                            if(j == 79){
                                JOptionPane.showMessageDialog (null, "You have gained Chrysler Cordoba!", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Chrysler_CordobaIcon);
                            }
                            else{
                                JOptionPane.showMessageDialog(null, "You have colected all 1 LVL cars", "Congralations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.MaxUpgradeIcon);
                            }
                        }

                    }
                }
            }
            }
            else{
                JOptionPane.showMessageDialog (null, "You don't have 1 LVL car frame!", "No Car Frame", JOptionPane.ERROR_MESSAGE, ImagesHolder.NoFrameIcon);}
            }
        else{
            JOptionPane.showMessageDialog (null, "You don't have available garage slot!", "No Garage Slot", JOptionPane.ERROR_MESSAGE, ImagesHolder.NoGarageIcon);
        }
        }
    else{
        JOptionPane.showMessageDialog (null, "You don't have enought player level!", "No Player Level", JOptionPane.ERROR_MESSAGE, ImagesHolder.NoPlayerIcon);
    }  
}   
if(Integer.parseInt(CarsLv1.cars[j])==0{

你应该这样读

if(Integer.parseInt(null)==0){


因为CarLv1.cars[j]在本例中为空。您需要修复此问题。您可能需要使用调试器并逐步执行,以便能够捕获j的准确值,只需捕获数字格式异常

try{
    int count = Integer.parseInt(CarsLv1.cars[j]);
    if(count == 0){
        for(int i=1;i <= 25;i+=2){ 
            ...
        }
    }
}catch(Exception e){}
试试看{
int count=Integer.parseInt(CarsLv1.cars[j]);
如果(计数=0){

对于(int i=1;i)为什么不在该行前面调用
System.out.println(CarsLv1.cars[j])
来查看哪个字符串返回该错误有可能存在
j
的值
为空?@DavidWallace不仅可能,根据异常情况,
CarsLvl.cars[j]肯定会发生这种情况
显然是空的。在这种情况下,答案是进行一些调试。您对数组的某个地方做出了错误的假设…并避免使用深度嵌套的if语句。了解
else
。了解
switch
语句。
package Clicker;

public class CarLevel1Holder {


    public String cars[] = new String[80];

    public CarLevel1Holder(){


    }

    public void CarsMaker(String a1,int k, String a2,int k1, String a3,int k2,String a4,int k3){

        cars[k] = a1;
        cars[k1] = a2;
        cars[k2] = a3;
        cars[k3] = a4;

    }

    public void MakeCars(){

        int u = 0;int i = 1;int o = 2;int p = 3;
        CarsMaker("Type", u,"Aston_Martin", i,"British",o, "0", p);   //Aston_Martin_DB9
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Acura", i,"Japanese",o, "0",p);          //Acura_NSX
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Ford", i,"American", o,"0",p);           //Ford_Mustang
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Moskvich", i,"Russian", o,"0",p);        //Moskvich_412
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Kia", i,"Korean",o,"0",p);               //Kia_Venga
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Fiat", i,"Italian", o,"0",p);            //Fiat_1500
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Ferrari", i,"Italian", o,"0",p);         //Ferrari_Enzo
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Aston_Martin", i,"British", o,"0",p);    //Aston_Martin_Rapide
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Koenigsegg", i,"Swedish", o,"0",p);      //Koenigsegg_CCX
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Honda", i,"Japanese", o,"0",p);          //Honda_Civic
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Cadilliac", i,"American", o,"0",p);      //Cadilliac_ATS
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Mitsubishi", i,"Japanese", o,"0",p);     //Mitsubishi_LancerEvolutionX
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Infiniti", i,"Japanese", o,"0",p);       //Infiniti_FX
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Lancia", i,"Italian", o,"0",p);          //Lancia_Thema
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Volvo", i,"Swedish", o,"0",p);           //Volvo_XC90
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Acura", i,"Japanese", o,"0",p);          //Acura_CSX
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Saab", i,"Swedish", o,"0",p);            //Saab_SonettII
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Renault", i,"French", o,"0",p);          //Renault_Floride
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"BMW", i,"Germany", o,"0",p);             //BMW_e34
         u += 4; i += 4; o +=4; p+= 4;
        CarsMaker("Type", u,"Chrysler", i,"American", o,"0",p);       //Chrysler_Cordoba
         u += 4; i += 4; o +=4; p+=4;




//      CarsLv1 Aston_Martin_DB9 = new CarsLv1("Type", "Aston_Martin", "British", 0);       
//      CarsLv1 Acura_NSX = new CarsLv1("Type", "Acura", "Japanese", 0);
//      CarsLv1 Ford_Mustang = new CarsLv1("Type", "Ford", "American", 0);
//      CarsLv1 Moskvich_412 = new CarsLv1("Type", "Moskvich", "Russian", 0);
//      CarsLv1 Kia_Venga = new CarsLv1("Type", "Kia", "Korean", 0);
//      CarsLv1 Fiat_1500 = new CarsLv1("Type", "Fiat", "Italian", 0);
//      CarsLv1 Ferrari_Enzo = new CarsLv1("Type", "Ferrari", "Italian", 0);
//      CarsLv1 Aston_Martin_Rapide = new CarsLv1("Type", "Aston_Martin", "British", 0);
//      CarsLv1 Koenigsegg_CCX = new CarsLv1("Type", "Koenigsegg", "Swedish", 0);
//      CarsLv1 Honda_Civic = new CarsLv1("Type", "Honda", "Japanese", 0);
//      CarsLv1 Cadilliac_ATS = new CarsLv1("Type", "Cadilliac", "American", 0);
//      CarsLv1 Mitsubishi_LancerEvolutionX = new CarsLv1("Type", "Mitsubishi", "Japanese", 0);
//      CarsLv1 Infiniti_FX = new CarsLv1("Type", "Infiniti", "Japanese", 0);
//      CarsLv1 Lancia_Thema = new CarsLv1("Type", "Lancia", "Italian", 0);
//      CarsLv1 Volvo_XC90 = new CarsLv1("Type", "Volvo", "Swedish",  0);
//      CarsLv1 Acura_CSX = new CarsLv1("Type", "Acura", "Japanese", 0);
//      CarsLv1 Saab_SonettII = new CarsLv1("Type", "Saab", "Swedish", 0);
//      CarsLv1 Renault_Floride = new CarsLv1("Type", "Renault", "French", 0);
//      CarsLv1 BMW_e34 = new CarsLv1("Type", "BMW", "Germany", 0);
//      CarsLv1 Chrysler_Cordoba = new CarsLv1("Type", "Chrysler", "American", 0);


    }

}
try{
    int count = Integer.parseInt(CarsLv1.cars[j]);
    if(count == 0){
        for(int i=1;i <= 25;i+=2){ 
            ...
        }
    }
}catch(Exception e){}