Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    The code is running when I'm running single thread but it throwing error when I'm trying to run multiple threads.

    -------------------------------------------------- MAIN------------------------------------------------------
     
    public class Main extends Thread
    {
     
    static class credit extends Thread
    {
        public void run()
        {
     
     
            String filename="Credit_card.txt";
            String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Credit_card.txt";
            String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Attribute_information.data";
     
            String dlimiter=",";
            Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attribute_Information,dlimiter,"training");
     
        }
    }   
    static class debt extends Thread
    {
        public void run()
        {
            String filename="Debt_consolidations.txt";
            String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Debt_consolidations.txt";
            String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Attribute_information.data";
     
            String dlimiter=",";
            Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attribute_Information,dlimiter,"training");
     
        }
     
    }
    static class education extends Thread
    {
        public void run()
        {
            String filename="Educational_loan.txt";
            String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Educational_loan.txt";
            String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Attribute_information.data";
     
            String dlimiter=",";
            Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attribute_Information,dlimiter,"training");
     
        }
    }
     
    static class home extends Thread
    {
        public void run()
        {
            String filename="Home_loan.txt";
            String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Home_loan.txt";
            String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Attribute_information.data";
     
            String dlimiter=",";
            Dataset training_DataSet= new Dataset(path_Training_DataSet,dlimiter,path_Attribute_Information,dlimiter,"training");
     
        }
     
     
    }
        public static void main(String args[])
        {
            long start=System.currentTimeMillis();
            try
            {
            String filename="Small_Business.txt";
            String path_Training_DataSet="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Small_Business.txt";
            String path_Attribute_Information="C:\\Users\\Kunal Pakhira\\Documents\\NetBeansProjects\\Project\\src\\Datasets\\Attribute_information.data";
     
            String dlimiter=",";
            Dataset training_dataset= new Dataset(path_Training_DataSet,dlimiter,path_Attribute_Information,dlimiter,"training");
     
     
              credit t1 =new credit();
              debt t2=new debt();
              education t3=new education();
              home t4=new home();
     
     
     
            t1.start();
            t2.start();
            t3.start();
            t4.start();
     
            t1.join();
            t2.join();        
            t3.join();        
            t4.join();
     
     
     
     
            }
            catch(Exception e)
            {
                System.out.println("Exception happened");
            }
     
     
            long end=System.currentTimeMillis();
            System.out.println("Required time="+(end-start));
     
        }
     
    }


    --------------------------------------------------- DATASET ------------------------------------------------------
     
    public class Dataset
    {
        int noOfRecords;
        int noOfAttributes;
        String[][] dataSet;
        String attributeInformation[][];
        String[] minValues;
        String[] maxValues;
        String[][] attributeValues;
        int noOfClasses;
        String[] classLevels;
        int noOfDifferentAttributeValues[];
        String[][] normalizedDataSet;
        String[][] normalizedAttributeValues;
        String valuesOfAttributes[];
        String changePoints[][];
        double informationGain[];
        String dominentClassLabel;
     
        Dataset(String dataFileName,String dataFileDlimiter,String attributeFileName,String attributeFileDlimiter,String type)
        {
     
            findNoOfRecords(dataFileName);
     
            findNoOfAttributes(dataFileName,dataFileDlimiter);
     
            insertRecordsInTwoDimentionalArray(dataFileName,dataFileDlimiter);
            if(type.equals("training"))
            {
     
                insertAttributeInformationsInTwoDimentionalArray(attributeFileName,attributeFileDlimiter);
     
                findMinValues();
     
                findMaxValues();
     
                storeSortedUniqueAttributeValues();
     
                findNoOfDifferentAttributeValues();
     
     
                getNumberOfClasses();
     
                findClassLevels();
     
                findDominantClassLevel();
     
                changePoints=findChangePoints();
     
                informationGain=calculateInformationGain();
     
            }
        }
     
        public void findNoOfRecords(String dataFileName)
        {
            FileUtil dataFileOriginal=new FileUtil(dataFileName);
            dataFileOriginal.readFile();
            noOfRecords=dataFileOriginal.noOfLines()-1;
        }
     
        public void showNoOfRecords()
        {
            System.out.println("noOfRecords="+noOfRecords);//for testing
        }
     
        public void findNoOfAttributes(String dataFileName,String dataFileDlimiter)
        {
            FileUtil dataFileOriginal=new FileUtil(dataFileName);
            dataFileOriginal.readFile();
            dataFileOriginal.readFile();
            String firstLine=dataFileOriginal.readLine(1,noOfRecords);
            IOUtil FL=new IOUtil(firstLine);
            FL.init_delim(dataFileDlimiter);
            noOfAttributes=0;
            while(FL.getNext()!=null)
            {
                noOfAttributes++;
            }
        }
     
        public void showNoOfAttributes()
        {
            System.out.println("noOfAttributes="+noOfAttributes);//for testing
        }
     
        public void insertRecordsInTwoDimentionalArray(String dataFileName,String dataFileDlimiter)
        {
            FileUtil dataFileOriginal=new FileUtil(dataFileName);
            dataSet=new String[noOfRecords][noOfAttributes];
            for(int outerLoopCounter=0;outerLoopCounter<=noOfRecords-1;outerLoopCounter++)
            {
                //System.out.println(outerLoopCounter);//for testing
                String record=dataFileOriginal.readLine(outerLoopCounter+1,noOfRecords);
                //System.out.println(record);//for testing
                IOUtil IO=new IOUtil(record);
                IO.init_delim(dataFileDlimiter);
                for(int innerLoopCounter=0;innerLoopCounter<=noOfAttributes-1;innerLoopCounter++)
                {
                    dataSet[outerLoopCounter][innerLoopCounter]=IO.getNext();
                }
            }
        }
        public void showDataset()
        {
            for(int outerLoopCounter=0;outerLoopCounter<=noOfRecords-1;outerLoopCounter++)
            {
                for(int innerLoopCounter=0;innerLoopCounter<=noOfAttributes-1;innerLoopCounter++)
                {
                    System.out.print(dataSet[outerLoopCounter][innerLoopCounter]+" ");
                }
                System.out.println("");
            }
        }
     
        public void insertAttributeInformationsInTwoDimentionalArray(String attributeFileName,String attributeFileDlimiter)
        {
            FileUtil attributeFileOriginal=new FileUtil(attributeFileName);
            attributeFileOriginal.readFile();
            int noOfLines=attributeFileOriginal.noOfLines()-1;
            attributeFileOriginal.readFile();
            String attributeFirstLine=attributeFileOriginal.readLine(1,noOfLines);
            IOUtil FL2=new IOUtil(attributeFirstLine);
            FL2.init_delim(attributeFileDlimiter);
     
            attributeInformation=new String[2][noOfAttributes];
            for(int outerLoopCounter=0;outerLoopCounter<=1;outerLoopCounter++)
            {
     
                String record=attributeFileOriginal.readLine(outerLoopCounter+1,noOfLines);
     
                IOUtil IO=new IOUtil(record);
                IO.init_delim(attributeFileDlimiter);
                for(int innerLoopCounter=0;innerLoopCounter<=noOfAttributes-1;innerLoopCounter++)
                {
                    attributeInformation[outerLoopCounter][innerLoopCounter]=IO.getNext();
                }
            }
        }
     
        public void showAttributeInformation()
        {
            for(int outerLoopCounter=0;outerLoopCounter<=2-1;outerLoopCounter++)
            {
                for(int innerLoopCounter=0;innerLoopCounter<=noOfAttributes-1;innerLoopCounter++)
                {
                    System.out.print(attributeInformation[outerLoopCounter][innerLoopCounter]+" ");
                }
                System.out.println("");
            }
        }
     
        public void findMinValues()
        {
            minValues=new String[noOfAttributes];
            for(int loopCounter=0;loopCounter<=noOfAttributes-1;loopCounter++)
            {            
                if(attributeInformation[1][loopCounter].equals("Float")||attributeInformation[1][loopCounter].equals("Integer"))
                {
                    int counter=-1;
                    do
                    {
                        counter++;                    
                        if(!dataSet[counter][loopCounter].equals("?"))
                            break;
                    }while(counter<(noOfRecords-1));
                    minValues[loopCounter]=dataSet[counter][loopCounter];
                    for(int loopCounter1=0;loopCounter1<=noOfRecords-1;loopCounter1++)
                    {      
                        if(!dataSet[loopCounter1][loopCounter].equals("?"))
                        {
                            if(Double.parseDouble(minValues[loopCounter])>Double.parseDouble(dataSet[loopCounter1][loopCounter]))
                            {
                                minValues[loopCounter]=dataSet[loopCounter1][loopCounter];
                            }
                        }
                    }
                }
            }
        }
     
        public void showMinValues()
        {
            for(int loopCounter=0;loopCounter<=noOfAttributes-1;loopCounter++)
            {
                if(attributeInformation[1][loopCounter].equals("Float")||attributeInformation[1][loopCounter].equals("Integer"))
                {
                    System.out.println("minValues["+loopCounter+"]="+minValues[loopCounter]);
                }
            }
        }
     
        public void findMaxValues()
        {
            maxValues=new String[noOfAttributes];
            for(int loopCounter=0;loopCounter<=noOfAttributes-1;loopCounter++)
            {
                if(attributeInformation[1][loopCounter].equals("Float")||attributeInformation[1][loopCounter].equals("Integer"))
                {
                    int counter=-1;
                    do
                    {
                        counter++;
                        if(!dataSet[counter][loopCounter].equals("?"))
                        break;
                    }while(counter<(noOfRecords-1));
                    maxValues[loopCounter]=dataSet[counter][loopCounter];
                    for(int loopCounter1=0;loopCounter1<=noOfRecords-1;loopCounter1++)
                    {
                        if(!dataSet[loopCounter1][loopCounter].equals("?"))
                        {
                            if(Double.parseDouble(maxValues[loopCounter])<Double.parseDouble(dataSet[loopCounter1][loopCounter]))
                            {
                                maxValues[loopCounter]=dataSet[loopCounter1][loopCounter];
                            }
                        }
                    }
                }
            }
        }
     
        public void showMaxValues()
        {
            for(int loopCounter=0;loopCounter<=noOfAttributes-1;loopCounter++)
            {
                if(attributeInformation[1][loopCounter].equals("Float")||attributeInformation[1][loopCounter].equals("Integer"))
                {
                    System.out.println("maxValues["+loopCounter+"]="+maxValues[loopCounter]);
                }
            }
        }
     
        public void storeSortedUniqueAttributeValues()
        {
            attributeValues=new String[noOfAttributes][noOfRecords+1];
            String Temp[]=new String[noOfRecords];
            boolean flag[]=new boolean[noOfRecords];
            for(int outerLoopCounter=0;outerLoopCounter<=noOfAttributes-1;outerLoopCounter++)
            {
                if(attributeInformation[1][outerLoopCounter].equals("Float")||attributeInformation[1][outerLoopCounter].equals("Integer"))
                {
     
                    String Temp2[]=new String[noOfRecords+1];
                    for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords-1;innerLoopCounter++)
                    {
                        Temp[innerLoopCounter]=dataSet[innerLoopCounter][outerLoopCounter];//to store attributes values
                        if(!Temp[innerLoopCounter].equals("?"))
                            flag[innerLoopCounter]=true;
                        else
                            flag[innerLoopCounter]=false;
                    }//end of for
                    boolean flag1=false;
                    int counter=0;
                    do
                    {
                        flag1=false;
                        String lowestAvaliableValue=maxValues[outerLoopCounter];
                        for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords-1;innerLoopCounter++)
                        {
                             if(flag[innerLoopCounter])
                             {                             
                                 if(Double.parseDouble(lowestAvaliableValue)>Double.parseDouble(Temp[innerLoopCounter]))
                                 {
                                     lowestAvaliableValue=Temp[innerLoopCounter];
                                     flag1=true;
                                 }                    
                             }
                        }
                        Temp2[counter]=lowestAvaliableValue;
     
                        counter++;
                        for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords-1;innerLoopCounter++)
                        {
                            if(lowestAvaliableValue.equals(Temp[innerLoopCounter]))
                            {
                                flag[innerLoopCounter]=false;
     
                            }
                        }
                    }while(flag1);
                    attributeValues[outerLoopCounter]=Temp2;
                }
                if(attributeInformation[1][outerLoopCounter].equals("String")||attributeInformation[1][outerLoopCounter].equals("Binary"))
                {                
                    String Temp2[]=new String[noOfRecords+1];
                    for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords-1;innerLoopCounter++)
                    {
                        Temp[innerLoopCounter]=dataSet[innerLoopCounter][outerLoopCounter];
                        if(!Temp[innerLoopCounter].equals("?"))
                            flag[innerLoopCounter]=true;
                        else
                            flag[innerLoopCounter]=false;
                    }
                    boolean flag1=false;
                    int counter=0;
                    do
                    {
                        flag1=false;
                        int innerLoopCounter1=0;
                        String firstAvaliableValue="";
                        do
                        {
                            firstAvaliableValue=Temp[innerLoopCounter1];
                            innerLoopCounter1++;
                            if(!firstAvaliableValue.equals("?"))
                                break;
                        }while(innerLoopCounter1<=(noOfRecords-1));                    
                        for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords-1;innerLoopCounter++)
                        {
                            if(flag[innerLoopCounter])
                            {
                                firstAvaliableValue=Temp[innerLoopCounter];
                                flag1=true;
                                break;
                            }
                        }
                        if(flag1)
                        {
                            Temp2[counter]=firstAvaliableValue;
                            counter++;
                            for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords-1;innerLoopCounter++)
                            {
                                if(firstAvaliableValue.equals(Temp[innerLoopCounter]))
                                {
                                    flag[innerLoopCounter]=false;
                                }
                            }
                        }
                    }while(flag1);
                    attributeValues[outerLoopCounter]=Temp2;                
                }     
            }
        }
        public void findNoOfDifferentAttributeValues()
        {
            noOfDifferentAttributeValues=new int[noOfAttributes];
            for(int outerLoopCounter=0;outerLoopCounter<=noOfAttributes-1;outerLoopCounter++)
            {
                int counter=0;
                for(int innerLoopCounter=0;innerLoopCounter<=noOfRecords;innerLoopCounter++)
                {
     
                    if(attributeValues[outerLoopCounter][innerLoopCounter]==null)
                    {
                        counter=innerLoopCounter;
                        break;
                    }
                }
                noOfDifferentAttributeValues[outerLoopCounter]=counter;
     
            }
        }
     
        public void showNoOfDifferentAttributeValues()
        {
            for(int counter1=0;counter1<=noOfAttributes-1;counter1++)
            {
                System.out.println("Number of different values for "+counter1+"th Attribute="+noOfDifferentAttributeValues[counter1]);
            }
        }
     
        public void showSorted()
        {
            System.out.println("Sorted Attributes Values");
            for(int counter1=0;counter1<=noOfAttributes-1;counter1++)
            {
                System.out.println("Attribute Number="+counter1);
                for(int counter2=0;counter2<=noOfRecords-1;counter2++)
                {
                     if(attributeValues[counter1][counter2]==null)
                        break;
                     else
                        System.out.print(attributeValues[counter1][counter2]);
                     System.out.print(",");
                }
                System.out.println("");
            }
        }
        public void getNumberOfClasses()
        {
            noOfClasses=0;
            for(int counter=0;counter<=noOfRecords-1;counter++)
            {
     
                if(attributeValues[noOfAttributes-1][counter]!=null)
                    noOfClasses++;
                else
                    break;
            }
        }
        public void showNoOfClasses()
        {
            System.out.println("No of Classes="+noOfClasses);
        }
        public void findClassLevels()
        {
            classLevels=new String[noOfClasses];
            for(int counter=0;counter<=noOfClasses-1;counter++)
            {
                classLevels[counter]=attributeValues[noOfAttributes-1][counter];
            }
        }
     
        public void showClassLevels()
        {
            System.out.println("Class Levels");
            for(int i=0;i<noOfClasses;i++)
                System.out.println(classLevels[i]);
        }
        public void findDominantClassLevel()
        {
            int classCounter[]=new int[noOfClasses];
            for(int recordNo=0;recordNo<=noOfRecords-1;recordNo++)
            {
                for(int classNo=0;classNo<=noOfClasses-1;classNo++)
                {
                    if(dataSet[recordNo][noOfAttributes-1].equals(classLevels[classNo]))
                    {
                        classCounter[classNo]++;
                        break;
                    }
                }
            }
            int maxClassCounter=0;
            for(int classNo=0;classNo<=noOfClasses-1;classNo++)
            {
                if(classCounter[classNo]>maxClassCounter)
                {
                    maxClassCounter=classCounter[classNo];
                    dominentClassLabel=classLevels[classNo];
                }
            }
        }
     
     
        public void showValuesOfAttributes()
        {
            for(int loopCounter=0;loopCounter<=noOfAttributes-1;loopCounter++)
            {
                System.out.println("Attribute["+loopCounter+"]="+valuesOfAttributes[loopCounter]);//for testing
            }
        }
        public String[][] findChangePoints()
        {
            String changePoints[][]=new String[noOfAttributes][];
            for(int outerloopCounter=0;outerloopCounter<=noOfAttributes-2;outerloopCounter++)
            {
                if(attributeInformation[1][outerloopCounter].equals("Float")||attributeInformation[1][outerloopCounter].equals("Integer"))
                {
                    String sortedAttributeAndClass[][]=sortRecordsAsPerAttributeValue(outerloopCounter,noOfAttributes,dataSet,attributeValues[outerloopCounter]);
                    int noOfChangePoints=findNoOfChangePoints(sortedAttributeAndClass);
                    //System.out.println("noOfChangePoints"+noOfChangePoints);//for testing
                    changePoints[outerloopCounter]=findArrayOfChangePoints(sortedAttributeAndClass,noOfChangePoints);                
                }
            }
            return changePoints;
        }
     
        String [][] sortRecordsAsPerAttributeValue(int attributeNo,int numberOfAttribute,String dataSet[][],String attributeValues[])
        {
            String sortedAttributeAndClass[][]=new String[2][dataSet.length];
            boolean flag[]=new boolean[dataSet.length];
            int counter=0;
            for(int outerloopCounter=0;outerloopCounter<=attributeValues.length-1;outerloopCounter++)
            {
                for(int innerloopCounter=0;innerloopCounter<=dataSet.length-1;innerloopCounter++)
                {
                    if(dataSet[innerloopCounter][attributeNo].equals(attributeValues[outerloopCounter])&&!flag[innerloopCounter])
                    {
                        sortedAttributeAndClass[0][counter]=dataSet[innerloopCounter][attributeNo];
                        sortedAttributeAndClass[1][counter]=dataSet[innerloopCounter][numberOfAttribute-1];
                        flag[innerloopCounter]=true;
                        counter++;
                    }
                }
            }
            counter=0;
            boolean flag2=false;
            for(int loopCounter=0;loopCounter<=dataSet.length-1;loopCounter++)
            {
     
                counter++;
                if(sortedAttributeAndClass[0][loopCounter]==null)
                {
                    flag2=true;
                    break;
                }
            }
            String sortedAttributeAndClass2[][];
            if(flag2)
            {
     
                sortedAttributeAndClass2=new String[2][counter-1];
            }
            else
            {
     
                sortedAttributeAndClass2=new String[2][counter];
            }
            for(int loopCounter=0;loopCounter<=sortedAttributeAndClass2[0].length-1;loopCounter++)
            {
                sortedAttributeAndClass2[0][loopCounter]=sortedAttributeAndClass[0][loopCounter];
                sortedAttributeAndClass2[1][loopCounter]=sortedAttributeAndClass[1][loopCounter];
            }
     
            return sortedAttributeAndClass2;
        }
     
        int findNoOfChangePoints(String sortedAttributeAndClass[][])
        {        
            int noOfChangePoints=0;
            String classLabel_1=sortedAttributeAndClass[1][0];
            String attributevalue_1=sortedAttributeAndClass[0][0];
            for(int loopCounter=1;loopCounter<=sortedAttributeAndClass[0].length-1;loopCounter++)
            {
     
                String classLabel_2=sortedAttributeAndClass[1][loopCounter];
                String attributevalue_2=sortedAttributeAndClass[0][loopCounter];
                if(!classLabel_1.equals(classLabel_2))
                {
                    if(!attributevalue_1.equals(attributevalue_2))
                    {
                        noOfChangePoints=noOfChangePoints+1;
     
                    }          
                }
                attributevalue_1=sortedAttributeAndClass[0][loopCounter];
                classLabel_1=sortedAttributeAndClass[1][loopCounter];
     
            }
                noOfChangePoints=noOfChangePoints+2;
            //}
     
            return noOfChangePoints;
        }
        String[] findArrayOfChangePoints(String sortedAttributeAndClass[][],int noOfChangePoints)
        {
            String arrayOfChangePointsWithDuplicateValues[]=new String[noOfChangePoints];
            arrayOfChangePointsWithDuplicateValues[0]=sortedAttributeAndClass[0][0];
            int counter=1;
            String attributevalue_1=sortedAttributeAndClass[0][0];
            String classLabel_1=sortedAttributeAndClass[1][0];
            for(int loopCounter=1;loopCounter<=sortedAttributeAndClass[0].length-1;loopCounter++)
            {
                String classLabel_2=sortedAttributeAndClass[1][loopCounter];
                String attributevalue_2=sortedAttributeAndClass[0][loopCounter];
                if(!classLabel_1.equals(classLabel_2))
                {
                    if(!attributevalue_1.equals(attributevalue_2))
                    {  
                        double a=Double.valueOf(attributevalue_1);
                        double b=Double.valueOf(attributevalue_2);
                        double c=((a+b)/2.0);
                        arrayOfChangePointsWithDuplicateValues[counter]=Double.toString(c);
                        //arrayOfChangePointsWithDuplicateValues[counter]=Double.toString(Double.valueOf(attributevalue_1)+Double.valueOf(attributevalue_2)/2.0);
                        //System.out.println(arrayOfChangePointsWithDuplicateValues[counter]);//for testing
                        counter++;
                    }//end of if
                }//end of if
                attributevalue_1=sortedAttributeAndClass[0][loopCounter];
                classLabel_1=sortedAttributeAndClass[1][loopCounter];
            }//end of for
            arrayOfChangePointsWithDuplicateValues[counter]=sortedAttributeAndClass[0][sortedAttributeAndClass[0].length-1];
            /*if(!attributevalue.equals(sortedAttributeAndClass[0][sortedAttributeAndClass[0].length-1]))
            {
                arrayOfChangePoints[counter]=sortedAttributeAndClass[0][sortedAttributeAndClass[0].length-1];
            }//end of if*/
            /*for(int loopCounter=0;loopCounter<=arrayOfChangePointsWithDuplicateValues.length-1;loopCounter++)
            {
                System.out.println(arrayOfChangePointsWithDuplicateValues[loopCounter]);//for testing
            }//end of for*/
            //to eliminate duplicate values
            noOfChangePoints=1;
            String point=arrayOfChangePointsWithDuplicateValues[0];
            for(int loopCounter=1;loopCounter<=arrayOfChangePointsWithDuplicateValues.length-1;loopCounter++)
            {
                if(!point.equals(arrayOfChangePointsWithDuplicateValues[loopCounter]))
                {
                    noOfChangePoints++;
                    point=arrayOfChangePointsWithDuplicateValues[loopCounter];
                }//end of if
            }//end of for        
            //System.out.println("noOfChangePoints="+noOfChangePoints);//for testing
            String arrayOfChangePoints[]=new String[noOfChangePoints];
            point=arrayOfChangePointsWithDuplicateValues[0];
            arrayOfChangePoints[0]=point;
            counter=1;
            for(int loopCounter=1;loopCounter<=arrayOfChangePointsWithDuplicateValues.length-1;loopCounter++)
            {
                if(!point.equals(arrayOfChangePointsWithDuplicateValues[loopCounter]))
                {
                    arrayOfChangePoints[counter]=arrayOfChangePointsWithDuplicateValues[loopCounter];
                    point=arrayOfChangePointsWithDuplicateValues[loopCounter];
                    counter++;
                }//end of if
            }//end of for
            return arrayOfChangePoints;
        }
        public double[] calculateInformationGain()
        {
            double entropy[]=calculateEntropy();
            double informationGain[]=new double[noOfAttributes-1];
            for(int attributeNo=0;attributeNo<=noOfAttributes-2;attributeNo++)
            {
                informationGain[attributeNo]=entropy[noOfAttributes-1]-entropy[attributeNo];
     
            }
            return informationGain;
        }
        public double[] calculateEntropy()
        {
     
            double classEntropy=calculateEntropyOfClass();
     
            double entropy[]=new double[noOfAttributes];
            entropy[noOfAttributes-1]=classEntropy;
            for(int attributeNo=0;attributeNo<=noOfAttributes-2;attributeNo++)
            {
                String attributesType=attributeInformation[1][attributeNo];
                if(attributesType.equals("Integer")||attributesType.equals("Float"))
                {
                    entropy[attributeNo]=calculateEntropyOfContinuousAttribute(attributeNo);
     
                }
                else if(attributesType.equals("Binary")||attributesType.equals("String"))
                {
                    entropy[attributeNo]=calculateEntropyOfCategoricalAttribute(attributeNo);
     
                }
            }
            return entropy;
        }
        public double calculateEntropyOfClass()
        {
            double probability_of_class[]=new double[noOfClasses];
            double classEntropy=0.0;
            for(int loopCounter=0;loopCounter<=noOfClasses-1;loopCounter++)
            {
                probability_of_class[loopCounter]=calculateProbabilityOfClass(classLevels[loopCounter]);
                classEntropy=classEntropy-probability_of_class[loopCounter]*Math.log(probability_of_class[loopCounter])/Math.log(2);
            }
            return classEntropy;
        }
        public double calculateProbabilityOfClass(String classLabel)
        {
            double classProbability=0.0;
            int classCount=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(classLabel.equals(dataSet[loopCounter][noOfAttributes-1]))
                    classCount++;
            }
            classProbability=(double)classCount/noOfRecords;
     
            return classProbability;
        }
        public double calculateEntropyOfContinuousAttribute(int attributeNo)
        {
     
            double average_interval_and_class_Entropy=0.0;
            int noOfIntervals=changePoints[attributeNo].length-1;
            for(int intervalNo=0;intervalNo<=noOfIntervals-1;intervalNo++)
            {            
                double minValue=Double.valueOf(changePoints[attributeNo][intervalNo]);            
                double maxValue=Double.valueOf(changePoints[attributeNo][intervalNo+1]);
     
                double interval_and_class_Entropy=0.0;
                if(intervalNo==0)
                {
                    for(int classNo=0;classNo<=noOfClasses-1;classNo++)
                    {
     
                        double probability_of_interval_and_class=calculateProbabilityOfFirstIntervalAndClass(classLevels[classNo],minValue,maxValue,attributeNo);
     
                        if(probability_of_interval_and_class>0.0)
                            interval_and_class_Entropy=interval_and_class_Entropy-probability_of_interval_and_class*Math.log(probability_of_interval_and_class)/Math.log(2);
                    }
                    int interval_value_count=calculateFirstIntervalValueCount(minValue,maxValue,attributeNo);
                    average_interval_and_class_Entropy=average_interval_and_class_Entropy+(double)interval_value_count/noOfRecords*interval_and_class_Entropy;
     
                }
                else
                {
                    for(int classNo=0;classNo<=noOfClasses-1;classNo++)
                    {
     
                        double probability_of_interval_and_class=calculateProbabilityOfIntervalAndClass(classLevels[classNo],minValue,maxValue,attributeNo);
     
                        if(probability_of_interval_and_class>0.0)
                            interval_and_class_Entropy=interval_and_class_Entropy-probability_of_interval_and_class*Math.log(probability_of_interval_and_class)/Math.log(2);
                    }
                    int interval_value_count=calculateIntervalValueCount(minValue,maxValue,attributeNo);
     
                    average_interval_and_class_Entropy=average_interval_and_class_Entropy+(double)interval_value_count/noOfRecords*interval_and_class_Entropy;
     
                }
            }
     
            return average_interval_and_class_Entropy;
        }
     
        public double calculateEntropyOfCategoricalAttribute(int attributeNo)
        {
     
            double average_attribute_and_class_Entropy=0.0;
            for(int attributeValueNo=0;attributeValueNo<=noOfDifferentAttributeValues[attributeNo]-1;attributeValueNo++)
            {
                double attribute_and_class_Entropy=0.0;
                for(int classNo=0;classNo<=noOfClasses-1;classNo++)
                {
                    double probability_of_attribute_and_class=calculateProbabilityOfAttributeAndClass(classLevels[classNo],attributeValues[attributeNo][attributeValueNo],attributeNo);
     
                    if(probability_of_attribute_and_class>0.0)
                        attribute_and_class_Entropy=attribute_and_class_Entropy-probability_of_attribute_and_class*Math.log(probability_of_attribute_and_class)/Math.log(2);                
                }
                int attribute_value_count=calculateAtttributeValueCount(attributeValues[attributeNo][attributeValueNo],attributeNo);
     
                average_attribute_and_class_Entropy=average_attribute_and_class_Entropy+(double)attribute_value_count/noOfRecords*attribute_and_class_Entropy;
            }
            return average_attribute_and_class_Entropy;
        }
        public double calculateProbabilityOfFirstIntervalAndClass(String classLabel,double min,double max,int attributeNo)
        {
            double intervalAndClassProbability=0.0;
            int intervalAndClassCount=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(!dataSet[loopCounter][attributeNo].equals("?"))
                {
                    if(classLabel.equals(dataSet[loopCounter][noOfAttributes-1])&&min<=Double.valueOf(dataSet[loopCounter][attributeNo])&&max>=Double.valueOf(dataSet[loopCounter][attributeNo]))
                    {
                        intervalAndClassCount++;
     
                    }
                }
            }
     
            intervalAndClassProbability=(double)intervalAndClassCount/noOfRecords;
     
            return intervalAndClassProbability;
        }
        public double calculateProbabilityOfIntervalAndClass(String classLabel,double min,double max,int attributeNo)
        {
     
            double intervalAndClassProbability=0.0;
            int intervalAndClassCount=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(!dataSet[loopCounter][attributeNo].equals("?"))
                {
                    if(classLabel.equals(dataSet[loopCounter][noOfAttributes-1])&&min<Double.valueOf(dataSet[loopCounter][attributeNo])&&max>=Double.valueOf(dataSet[loopCounter][attributeNo]))
                    {
                        intervalAndClassCount++;
     
                    }
                }//end of if
            }//end of for
     
            intervalAndClassProbability=(double)intervalAndClassCount/noOfRecords;
     
            return intervalAndClassProbability;
        }
        public double calculateProbabilityOfAttributeAndClass(String classLabel,String attributeValue,int attributeNo)
        {
            double attributeAndClassProbability=0.0;
            int attributeAndClassCount=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(classLabel.equals(dataSet[loopCounter][noOfAttributes-1])&&attributeValue.equals(dataSet[loopCounter][attributeNo]))
                    attributeAndClassCount++;
            }
            attributeAndClassProbability=(double)attributeAndClassCount/noOfRecords;
     
            return attributeAndClassProbability;
        }
        public int calculateAtttributeValueCount(String attributeValue,int attributeNo)
        {
            int attribute_value_count=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(attributeValue.equals(dataSet[loopCounter][attributeNo]))
                    attribute_value_count++;
            }
            return attribute_value_count;
        }
        public int calculateFirstIntervalValueCount(double min,double max,int attributeNo)
        {
            int interval_value_count=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(!dataSet[loopCounter][attributeNo].equals("?"))
                {
                    if(min<=Double.valueOf(dataSet[loopCounter][attributeNo])&&max>=Double.valueOf(dataSet[loopCounter][attributeNo]))
                        interval_value_count++;
                }
            }
     
            return interval_value_count;
        }
        public int calculateIntervalValueCount(double min,double max,int attributeNo)
        {
            int interval_value_count=0;
            for(int loopCounter=0;loopCounter<=noOfRecords-1;loopCounter++)
            {
                if(!dataSet[loopCounter][attributeNo].equals("?"))
                {
                    if(min<Double.valueOf(dataSet[loopCounter][attributeNo])&&max>=Double.valueOf(dataSet[loopCounter][attributeNo]))
                        interval_value_count++;
                }
            }
     
            return interval_value_count;
        }
        public void showInformationGain()
        {
            for(int attributeNo=0;attributeNo<=noOfAttributes-2;attributeNo++)
            {
                System.out.println("Information gain of Attribute No"+attributeNo+"="+informationGain[attributeNo]);
            }
        }
    }

    ------------------------------------------------- FILEUTIL--------------------------------------------------
    import java.io.*;
    import java.util.*;
    public class FileUtil extends IOUtil
    {
        static private BufferedReader buf;    
        static private BufferedWriter outbuf;
        private FileReader infile;
        private FileWriter outfile;
        String FileName;
     
        public FileUtil(String file) 
        {   
            super("FILE");
            this.FileName = file;        
        }
        public void readFile()
        { 
            try 
            {      
                infile = new FileReader(FileName);
            }
            catch (FileNotFoundException nf) 
            {     
                System.out.println("File Not Found"); 
            }
            buf = new BufferedReader(infile);  
        }
        public void writeFile() 
        {   
            try 
            {     
                outfile = new FileWriter(FileName);  
            }
            catch (IOException er)
            {    
                System.out.println("Write Error"); 
            }
            outbuf = new BufferedWriter(outfile);
        } 
     
        synchronized public String readLine(int lineNo,int noOfRecords)
        {
            String aLine=null;        
            this.readFile();
            for(int i=1;i<=noOfRecords;i++)
            {
                if(i==lineNo)
                {
                    try
                    {
                        aLine=this.buf.readLine();
     
                    }
                    catch (Throwable t)
                    {
                        System.out.println("catch:"+t);
                        System.out.println("readline not performed");
                    }
                }
                else
                {
                    try
                    {
                        this.buf.readLine();    
                    }
                    catch (Throwable t)
                    {
                        ;
                    }
                }
            }
            return aLine;
        }
        public int noOfLines()
        {
                int noOfRecords=0;
                try
                {
                    this.readFile();
     
                    while(this.buf.readLine()!=null)
                    {
                        noOfRecords++;
                    }
                }
                catch (Throwable t)
                {
                    ;
                }    
            return noOfRecords;
        }
         public static void main(String args[])
         {
             try 
             {
                 FileUtil fileOriginal=new FileUtil("C:/Documents and Settings/Administrator/attributeSelection/attributeSelection/src/IRIS/INPUT_FILES/Original.data");
                 for(int i=1;i<=14;i++)
                 {
                     System.out.println(fileOriginal.readLine(i,14));
                 }
              }
              catch (Throwable t)
              {
                 ;
              } 
         }
    }

    -------------------------------------- IOUTIL-------------------------------------------------------
    import java.io.*;
    import java.util.*;
    public class IOUtil
    {  
        String IOString;
        StringTokenizer Tk;
        String delim;
        String S="";
     
        public IOUtil(String s) 
        {    
            this.IOString = s ; 
        }
        public String readString() throws IOException
        {  
            if (Tk==null) 
                Tk = new StringTokenizer (IOString,delim);   
            while(true)
            {    
                try
                {       
                    return Tk.nextToken();    
                }
                catch (NoSuchElementException e1)
                { 
                    throw new IOException(); 
                }
            }
        }
       public String findNextWord(String s)
        {    
            do 
            {  
                try 
                {	
                    S = readString();    
                }
                catch (IOException io)
                {	
                    System.out.println("Input Error"); 
                }        
            }while(!S.equals(s) && !S.equals(""));   
            if(!S.equals(""))
            {      
                try 
                {	
                    S = readString();    
                }
                catch (IOException io) 
                {
                    System.out.println("Input Error");      
                } 
            }
            return S;  
        }
        public String getNext() 
        {    
            try 
            {   
                S = readString();  
            }
            catch (IOException io) 
            {    
     
                S=null;
            }
            return(S); 
        }
         public void init_delim(String s) 
        {   
            delim = s;  
        }
         public static void main(String args[])
         {
             IOUtil IO=new IOUtil("XYZ");
             IO.init_delim(" ");
             for(int i=0;i<4;i++)
             {
                System.out.println(IO.getNext());
             }
     
         }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    Please copy the full text of the error message and paste it here so we can see where the error is.

    Where is the String variable given a value so that it is not null?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here so we can see where the error is.

    Where is the String variable given a value so that it is not null?
    [QUOTE] How to add pictures from computer ? So I can add pictures for better understanding ? PS. I am new to Java Programming Forums [/QUOTES]

    --- Update ---

    [QUOTE=dejavu48;172183]
    How to add pictures from computer ? So I can add pictures for better understanding ? PS. I am new to Java Programming Forums

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    Please do not post images. Text can not be copied from an image for responses or for doing a search.
    Copy the text of the error message and paste it here.

    What String variable has the null value? Where is that variable supposed to be given a value?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    [b] This is the error I'm getting [\b]

    [exception]

    Exception in thread "Thread-1" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:197)
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:219)
    at newpackage.IOUtil.readString(IOUtil.java:19)
    at newpackage.IOUtil.getNext(IOUtil.java:62)
    at newpackage.Dataset.insertRecordsInTwoDimentionalAr ray(Dataset.java:102)
    at newpackage.Dataset.<init>(Dataset.java:29)
    at newpackage.Main$debt.run(Main.java:31)

    Exception in thread "Thread-3" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:197)
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:219)
    at newpackage.IOUtil.readString(IOUtil.java:19)
    at newpackage.IOUtil.getNext(IOUtil.java:62)
    at newpackage.Dataset.insertRecordsInTwoDimentionalAr ray(Dataset.java:102)
    at newpackage.Dataset.<init>(Dataset.java:29)
    at newpackage.Main$home.run(Main.java:59)

    Exception in thread "Thread-0" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:197)
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:219)
    at newpackage.IOUtil.readString(IOUtil.java:19)
    at newpackage.IOUtil.getNext(IOUtil.java:62)
    at newpackage.Dataset.insertRecordsInTwoDimentionalAr ray(Dataset.java:102)
    at newpackage.Dataset.<init>(Dataset.java:29)
    at newpackage.Main$credit.run(Main.java:18)
    Exception in thread "Thread-2" java.lang.NullPointerException
    Required time=3429

    [\exception]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    What String has the null value? Do some debugging to find the String with the null value.
    Then backtrack in the code to see why the String does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    When I'm running it with one thread it is running smoothly, it is not giving any error. But when I'm running 4 threads, it is giving the above said error.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    Did you find the String with the null value?

    What version of java are you using?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    Quote Originally Posted by Norm View Post
    Did you find the String with the null value?

    What version of java are you using?

    No I didn't found the String with the null value.
    java version "16.0.1" 2021-04-20
    Java(TM) SE Runtime Environment (build 16.0.1+9-24)

    --- Update ---

    Quote Originally Posted by Norm View Post
    What String has the null value? Do some debugging to find the String with the null value.
    Then backtrack in the code to see why the String does not have a valid value.
    When we are running sequentially it is not generating any nullPointerException. Multiple child threads is generating nullPointerException. One of the threads getting a value of a record as null suddenly.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    No I didn't found the String with the null value.
    at java.base/java.util.StringTokenizer.<init>(StringTokenizer.j ava:219)
    at newpackage.IOUtil.readString(IOUtil.java:19)
    The error message shows that the null value is being passed to the StringTokenizer's constructor.
    Can you add code there to detect that null value and to not pass it to the constructor?

    A possible source of problems is the static variables in the FileUtil class. Remove the static declaration for those variables so that each instance of the FileUtil class gets its own values instead of sharing them.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    dejavu48 (July 25th, 2021)

  12. #11
    Junior Member
    Join Date
    Jul 2021
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null

    Yes now its working, Thank you !!

Similar Threads

  1. "Exception in thread "main" java.lang.NullPointerException" help?
    By redstripes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2014, 08:59 AM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM

Tags for this Thread