Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a problem with my main class when I build my project..

I get message :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ocr.training.MnistTraining.main(MnistTraining.java:22)
Java Result: 1

My Java code:

Java
public class MnistTraining {
    
    /*
     * ARGS Organization:
     * 0 -> Number of Train Images
     * 1 -> Number of cells for HorizontalCelledProjection
     * 2 -> Number of cells for VerticalCelledProjection
     */
    public static void main(String[] args) throws IOException{ 
        int numTrainImages = new Integer(args[0]);
        double[][] actual = new double[numTrainImages][];
        double[][] ideal = new double[numTrainImages][];
        MnistManager m = new MnistManager(Config.MNIST_TRAIN_IMAGES, Config.MNIST_TRAIN_LABELS);
        
        FeatureExtraction fe = FeatureExtractionBuilder
                                .create()
                                .children(new HorizontalCelledProjection(new Integer(args[1])), 
                                          new VerticalCelledProjection(new Integer(args[2])))
                                .build();
        
        // Build Training Data
        for(int i = 1; i <= numTrainImages; ++i) {
            // Get Pixel Matrix
            m.setCurrent(i);
            int[][] image = m.readPixelMatrix();
            
            fe.setPixelMatrix(image);
            fe.compute();
            
            // Add to Training Data
            double[] idealVector = new double[Config.OUTPUT_NEURONS];
            idealVector[m.readLabel()] = 1;
            
            actual[i-1] = fe.getFeatureVector();
            ideal[i-1] = idealVector;
        }
        
        int inputNeurons = fe.getFeatureVector().length;
        int hiddenNeurons = (2/3) * inputNeurons;
        
        NeuralNetwork nn = NeuralNetworkBuilder
                                .create()
                                .inputNeurons(inputNeurons)
                                .hiddenNeurons(hiddenNeurons)
                                .outputNeurons(Config.OUTPUT_NEURONS)
                                .build();
        
        nn.trainNetwork(actual, ideal);
        
        nn.persistNetwork();
        
    }
    
}


This The problem: line 22

int numTrainImages = new Integer(args[0]);


What happened and how to solve it, Help me... thank you
Posted
Updated 24-Jan-18 23:49pm

1 solution

It simply means that your args object is not null, but it is empty. Your entry-point method (main) received 0 command-line arguments. What to do? Check for the size of the array before indexing it.

—SA
 
Share this answer
 
Comments
Jv Doe 22-Dec-13 1:00am    
Thank you Sergey Alexandrovich Kryukov for your solution,. but I am not so familiar with what you mean by calculating the size of the array and how to index. Do you think the code is correct or should there be adding something?

I apologize for not really understand about it, but I try to keep learning ..
Sergey Alexandrovich Kryukov 22-Dec-13 1:42am    
Let's say you expect 1 argument in command line and want to use it. But the user may or may not provide this argument. So, you check up
if (args.length > 0) {
processCommandLineArgument(args[0]);
} else {
// don't do it
// do something else
}

—SA
Jv Doe 22-Dec-13 8:30am    
Thank you Sergey Alexandrovich Kryukov.
Sergey Alexandrovich Kryukov 22-Dec-13 11:59am    
You are welcome.
—SA
Member 11769199 25-Jan-18 5:22am    
hello guys I am working on speech recognition using sphinx 4 and i tried to develop live speech recognition interface but java says at runtime error
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 101"
when i used small "myLanguageModel.lm" it also shows error
"NullPointerException" I tried both eclipse and netbeans, my jar files are sphinx4 -5prealpha and downloaded from known site of sphinx. pls help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900