Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Daniel Alievsky

Thread: main method error.

  1. #1
    joehakarashi is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default main method error.

    Hi guys, sorry my English quite terrible and also new in this forum, i don't know about forum rules, sorry =(
    i want ask something guys.
    this is my code:

    Java Code:
    //Super Kelas
    public class MalayKuih 
    {
        protected String name;
        
        public MalayKuih(String n)
        {
            name = n;
        }
        
        public void displayHowToCook()
        {
            System.out.println("Depends on the type of kuih");
        }
        
       //Sub Kelas Malay Kuih
        public class KuihBakar extends MalayKuih
        {
            
            public KuihBakar()
            {
                super("Kuih bakar");
            }
            
            public void displayHowToCook()
            {
                System.out.println(name + "is cooked by baking");
            }
        }
        
         //Sub Kelas Malay Kuih
        public class Karipap extends MalayKuih
        { 
           
            public Karipap(String name)
            {
                super(name);
            }
            
            public void displayHowToCook()
            {
                System.out.println(name +"is cooked by frying");
            }
            
            public void displayFilling()
            {
                System.out.println("Depends on the type of karipap");
            }
        }
        
        
         //Sub Kelas Malay Kuih
        public class LepatPisang extends MalayKuih
        { 
           
            public LepatPisang()
            {
                super("Lepat Pisang");
            }
            
            public void displayHowToCook()
            {
                System.out.println(name + "is cooked by steaming");
            }
        }
        
        
        //Sub Kelas Karipap
        public class KaripapSardin extends Karipap
        {
            public KaripapSardin()
            {
                super("Karipap sardin");
            }
            
            public void displayFilling()
            {
                System.out.println(name + "is filled with sardine");
            }
        }
        
        
        //Sub Kelas Karipap
        public class KaripapAyam extends Karipap
        {
            public KaripapAyam()
            {
                super("Karipap Ayam");
            }
            
            public void displayFilling()
            {
                System.out.println(name +"if filled with chicken");
            }
        }
    
    }
    my question is when i create an object for "KuihBakar" in netbean on other file, not in same file, compile shows error.
    this is my example:
    Java Code:
    class Test 
    {
       public static void main(String [] args)
       {
         KuihBakar a = new KuihBakar();
         
         LepatPisang b = new LepatPisang();
                 
       } 
    }
    errors on "KuihBakar();" and "LepatPisang();"


    sorry for posting without a rules and for bad English.
    Attached Thumbnails Attached Thumbnails main method error.-capture.jpg  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: main method error.

    If there are error messages, please copy the full text and paste it here. Not an image because text can't be copied from an image to be included in any response.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Dec 2013
    Location
    Israel, Karmiel
    Posts
    4
    Rep Power
    0

    Default Re: main method error.

    It seems you made 2 mistakes here.

    1) If a class is declared inside another class, then, in most cases, you should add "static" specifier. Your classes "KuihBakar", "Karipap", "LepatPisang", "KaripapSardin", "KaripapAyam" are declared as parts of "MalayKuih" class, so, you need to declare them "static", for example:
    public static class KuihBakar extends MalayKuih {...
    Without static specifier, your declaration means much more complex linkage between inner and outer classes: every instance of an inner class has a built-in invisible link to an instance of the outer class and, so, has an ability to access members of an instance of outer class. As a result, constructing such classes is little more complicated. You should read about this in a tutorial; in most cases, static classes are more suitable and much more simple solution.

    2) To create an instance of an inner static classes, you should specify also the outer class:
    Java Code:
         MalayKuih.KuihBakar a = new MalayKuih.KuihBakar();
         MalayKuih.LepatPisang b = new MalayKuih.LepatPisang();
    With such two corrections, your code is compiled successfully. My advice: do not use inner classes until you will quite understand this feature. In you case, the best solution is creating 7 separate java-files, where each class is placed in the corresponing file, and placing all files into some package.
    joehakarashi likes this.

  4. #4
    joehakarashi is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: main method error.

    With such two corrections, your code is compiled successfully. My advice: do not use inner classes until you will quite understand this feature. In you case, the best solution is creating 7 separate java-files, where each class is placed in the corresponing file, and placing all files into some package.[/QUOTE]

    solved, i create 7 separate java-file
    thanks... =D
    but if i use static just like u said, will it work?

  5. #5
    joehakarashi is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: main method error.

    Quote Originally Posted by Norm View Post
    If there are error messages, please copy the full text and paste it here. Not an image because text can't be copied from an image to be included in any response.
    i'm sorry admin, like i said i'm new here...my bad =(

  6. #6
    joehakarashi is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: main method error.

    Quote Originally Posted by joehakarashi View Post
    Hi guys, sorry my English quite terrible and also new in this forum, i don't know about forum rules, sorry =(
    i want ask something guys.
    this is my code:

    Java Code:
    //Super Kelas
    public class MalayKuih 
    {
        protected String name;
        
        public MalayKuih(String n)
        {
            name = n;
        }
        
        public void displayHowToCook()
        {
            System.out.println("Depends on the type of kuih");
        }
        
       //Sub Kelas Malay Kuih
        public class KuihBakar extends MalayKuih
        {
            
            public KuihBakar()
            {
                super("Kuih bakar");
            }
            
            public void displayHowToCook()
            {
                System.out.println(name + "is cooked by baking");
            }
        }
        
         //Sub Kelas Malay Kuih
        public class Karipap extends MalayKuih
        { 
           
            public Karipap(String name)
            {
                super(name);
            }
            
            public void displayHowToCook()
            {
                System.out.println(name +"is cooked by frying");
            }
            
            public void displayFilling()
            {
                System.out.println("Depends on the type of karipap");
            }
        }
        
        
         //Sub Kelas Malay Kuih
        public class LepatPisang extends MalayKuih
        { 
           
            public LepatPisang()
            {
                super("Lepat Pisang");
            }
            
            public void displayHowToCook()
            {
                System.out.println(name + "is cooked by steaming");
            }
        }
        
        
        //Sub Kelas Karipap
        public class KaripapSardin extends Karipap
        {
            public KaripapSardin()
            {
                super("Karipap sardin");
            }
            
            public void displayFilling()
            {
                System.out.println(name + "is filled with sardine");
            }
        }
        
        
        //Sub Kelas Karipap
        public class KaripapAyam extends Karipap
        {
            public KaripapAyam()
            {
                super("Karipap Ayam");
            }
            
            public void displayFilling()
            {
                System.out.println(name +"if filled with chicken");
            }
        }
    
    }
    my question is when i create an object for "KuihBakar" in netbean on other file, not in same file, compile shows error.
    this is my example:
    Java Code:
    class Test 
    {
       public static void main(String [] args)
       {
         KuihBakar a = new KuihBakar();
         
         LepatPisang b = new LepatPisang();
                 
       } 
    }
    errors on "KuihBakar();" and "LepatPisang();"


    sorry for posting without a rules and for bad English.


    Daniel I have another problem, this is my main method, how can i put the object created in "LepatPisang" into the array created in "KaripapPisang"
    main method
    Java Code:
    class Test 
    {
       public static void main(String [] args)
       {
           
           
        KuihBakar kuih = new KuihBakar();
        KaripapSardin kuih1 = new KaripapSardin();
        KaripapAyam kuih2 = new KaripapAyam();
        LepatPisang kuih3 = new LepatPisang();
        
        
        MalayKuih[] kuih4 = new MalayKuih[4];       
        kuih4[0] = new  KuihBakar();
        kuih4[1] = new  KaripapSardin();
        kuih4[2] = new  KaripapAyam();
        kuih4[3] = new  LepatPisang();
        
        
        int z;
        for (z=0; z<4; z++)
        {
            kuih4[z].displayHowToCook();
        }
        
        if(kuih4[z] instanceof Karipap)
        {
            Karipap kuih5 = new Karipap(kuih4[z].name);
            kuih5.displayFilling();
        }
        
       }
    }

    this is error code after i run the program:

    run:
    Kuih bakaris cooked by baking
    Karipap sardinis cooked by frying
    Karipap Ayamis cooked by frying
    Lepat Pisangis cooked by steaming
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at Test.main(Test.java:26)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: main method error.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at Test.main(Test.java:26)
    At line 26 the code uses an array index (4) that is past the end of the array. Look at line 26 and see why the index went past the end.
    Remember that array indexes range in value from 0 to the array length-1.

    Also the value of a for loop's control variable will be what caused the for loop to exit. If the loop control goes from 0 to 3 it will be 4 when the loop exits. Why are you defining and using z outside of the for loop?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    joehakarashi is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: main method error.

    Quote Originally Posted by Norm View Post
    At line 26 the code uses an array index (4) that is past the end of the array. Look at line 26 and see why the index went past the end.
    Remember that array indexes range in value from 0 to the array length-1.

    Also the value of a for loop's control variable will be what caused the for loop to exit. If the loop control goes from 0 to 3 it will be 4 when the loop exits. Why are you defining and using z outside of the for loop?
    Java Code:
    class Test 
    {
       public static void main(String [] args)
       { 
        KuihBakar kuih = new KuihBakar();
        KaripapSardin kuih1 = new KaripapSardin();
        KaripapAyam kuih2 = new KaripapAyam();
        LepatPisang kuih3 = new LepatPisang();
        
        Karipap[] sedap = new Karipap[3];
        
        MalayKuih[] kuih4 = new MalayKuih[4];       
        kuih4[0] = new  KuihBakar();
        kuih4[1] = new  KaripapSardin();
        kuih4[2] = new  KaripapAyam();
        kuih4[3] = new  LepatPisang();
        
        sedap[1] = new KaripapSardin();
        sedap[2] = new KaripapAyam();
        
        
        for (int z=0; z<4; z++)
        {
            kuih4[z].displayHowToCook();
        
       
        if(kuih4[z] instanceof Karipap)
        {
            sedap[z].displayFilling();
        }
      
        }
       }
    }
    oh, i just want to do a little bit experiment. hehe
    it's finished for my fourth assignment tutorial in java.
    thanks mod. oh by the way, can i know about forum rules. can u send a link? Please.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: main method error.

    Java Code:
       
        for (int z=0; z<4; z++)
    You should use the array's length field to control the loop, not a hardcoded 4:
    Java Code:
       
        for (int z=0; z<theArrayNameHere.length; z++)
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: main method error.

    Quote Originally Posted by joehakarashi View Post
    thanks mod. oh by the way, can i know about forum rules. can u send a link? Please.
    Wow, someone actually asks for them! Very good.

    Guide For New Members

    There are many more unwritten 'rules' that are not written down because common decency and common sense should already prevent you from breaking them. So no need for them to be explained to you, because I assume you are decent and have common sense ;)
    Last edited by gimbal2; 12-09-2013 at 02:45 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    joehakarashi is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: main method error.

    Quote Originally Posted by Norm View Post
    Java Code:
       
        for (int z=0; z<4; z++)
    You should use the array's length field to control the loop, not a hardcoded 4:
    Java Code:
       
        for (int z=0; z<theArrayNameHere.length; z++)
    done my prof. suggest that array name should added with .length
    and
    Java Code:
     if(kuih4[z] instanceof Karipap)
    also should add
    Java Code:
    Karipap sedap = (Karipap) kuih4[z];
    nice thanks

Similar Threads

  1. Error when adding an array into main method
    By abeidson in forum New To Java
    Replies: 7
    Last Post: 03-12-2013, 05:49 PM
  2. Error: Main method not found! Please help!
    By xemkruz2012 in forum New To Java
    Replies: 2
    Last Post: 03-12-2013, 12:19 AM
  3. Replies: 8
    Last Post: 11-22-2012, 08:16 AM
  4. main method not found error in class . wat s the error?
    By kirankumaragnihotram in forum New To Java
    Replies: 10
    Last Post: 06-29-2012, 12:26 PM
  5. Replies: 2
    Last Post: 09-27-2011, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •