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: throws Exception error

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default throws Exception error

    In my neatbens project I have two java class

    First java class have two function.
    public void e()
    public static void f(String[] args) throws Exception


    second java class named rezultati have functions like:
    public void a()
    public void b()
    public void c()
    public void d(String[] args) throws Exception

    if in first java class in function f i use comand:

    rezultati objekt = new rezultati();
    objekt.d(args) or objekt.a(), or objekt.b()

    its ok but if I like use command objekt.d(args) in public void e() I get error. How I can call objekt.d(args) from ublic void e()?

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: throws Exception error

    You should be able to. So you must not be providing sufficient information. Please construct a small example that has complete code that demonstrates the problem.

    Regards,
    Jim

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: throws Exception error

    I get error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2017
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: throws Exception error

    I get error message:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static method sestej() cannot be referenced from a static context
    at preprosti.Preprosti.main(Preprosti.java:27)
    C:\Users\Igor\AppData\Local\NetBeans\Cache\8.2\exe cutor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 1 second)

    I send link with example
    https://www.dropbox.com/s/dcyehl7dyv...rosti.zip?dl=0

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: throws Exception error

    non-static method sestej() cannot be referenced from a static context
    The method sestej is an instance method that can only be called when an instance of the class exists using a reference to that instance.
    It can not be called from a static method without using a reference to the instance of the class.
    For example:
       rezultati objekt = new rezultati();    // create instance of class
       objekt.d(args);                               // use reference to instance to call method
     
       d(args);                                        // not allowed if this code is in static method

    I send link with example
    Please post all code and examples here in the thread, not via a link
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2017
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: throws Exception error

    public class Preprosti {
         public  void prikaz()  {
             rezultati objekt = new rezultati();
           String cunam = objekt.poveja(args); // dont work normal
            System.out.println(cunam);
     
         }
        public static void main(String[] args) throws Exception {
           rezultati objekt = new rezultati();
           int izracunam = objekt.sestej(5, 8); // work normal
            System.out.println(izracunam);
            String cunam = objekt.poveja(args); // work normal
            System.out.println(cunam);
           objekt.skrij(args); // work normal
          prikaz(); // dont work normal
     
        }
        }

    function , sestej, poveja in java class rezultati;

     public int sestej(int a, int b){
        int c = a+b;
        return c;
     
    }
        public  String poveja(String [] args) throws Exception {
            String ime;
            ime ="avtomobil";
            return ime;
        }
    Last edited by Norm; November 19th, 2018 at 04:41 PM. Reason: Removed space in ending code tag

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: throws Exception error

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Do you have any java programming questions?

    What does "dont work normal" mean? If there are error messages, please copy the full text and pasted it here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Dec 2017
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: throws Exception error

     
    javni razred Preprosti { 
    public void prikaz () { 
    rezultati objekt = novi rezultati (); 
    String cunam = objekt.poveja (args); // ne deluje normalno 
    System.out.println (cunam); 
     
    } 
    public static void main (String [] args) vrže Izjema { 
    rezultati objekt = novi rezultati (); 
    int izracunam = objekt.sestej (5, 8); // delo normalno 
    System.out.println (izracunam); 
    String cunam = objekt.poveja (args); // delo normalno 
    System.out.println (cunam); 
    objekt.skrij (args); // delo normalno 
    prikaz (); // ne deluje normalno 
     
    } 
    }

    funkcijo, sestej, poveja v java class rezultate;

    public int sestej (int a, int b) { 
    int c = a + b; 
    vrnitev c; 
     
    } 
    javni ukaz String poveja (String [] args) vrže izjemo { 
    String ime; 
    ime = "avtomobil"; 
    vrniti ime; 
    }
    Last edited by Norm; November 19th, 2018 at 04:43 PM. Reason: Removed space from end code tag 2X

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: throws Exception error

    What happened to the code's indentations? All the source statements should not start in the first column.

    Does the code get compiler error messages? Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Dec 2017
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: throws Exception error

    for me this code work normal.

         rezultati objekt = new rezultati();
             String[] args = null;
             try {
                 objekt.skrij(args);
             } catch (Exception ex) {
                 Logger.getLogger(Preprosti.class.getName()).log(Level.SEVERE, null, ex);
             }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: throws Exception error

    Did you have any java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ExecuteUpdate throws exception
    By bodylojohn in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 2nd, 2014, 04:17 AM
  2. [SOLVED] TableRowSorter throws Exception
    By Onur in forum AWT / Java Swing
    Replies: 1
    Last Post: November 7th, 2013, 06:23 AM
  3. Singly Linked List of Integers, get(int i) function throws Null Pointer Exception
    By felixtum2010 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 06:55 PM
  4. ZipInputStream Throws Illegalargument exception for diacritics
    By meghaladevi in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 12th, 2010, 02:32 AM
  5. [SOLVED] File.createTempFile() throws exception on Win7
    By jitinsingla in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 10th, 2010, 02:14 PM