Methods not visible in self made library

edited November 2016 in Library Questions

Hi.

I have made a library to enable some quicker and easier interaction. I made it in eclipse, which does not find any errors. I have also tried to import it into Processing, which at least does not seem to give an errors about importing the library or declaring class instances. But it can not use any of the methods defined in the library. For example, when I try to call a constructor:

import quickInteraction.*; QuickInteraction qi = new QuickInteraction(this);

I get the error "The constructor QuickInteraction(PApplet) is not visible". However I know that the constructor with that parameter are in the library and both class and constructor are public.

If anyone can help, please do. And if more code pieces from the library are needed, please ask, and I shall deliver. Thank you.

Answers

  • "is not visible"

    Did you make it public?

    Rule of thumb -- if you are used to writing in Processing, get used to writing public in front of almost everything in your Java library.

  • Ack -- sorry, you said it was public already, I somehow missed that part.

    Cut a test copy of your library down to a single empty class with that constructor. Do you get the same error? If so, share the code here.

  • edited November 2016

    So, I removed all other classes from the .jar file than the QuickInteraction.class. I tested it again as before but added one of the missing classes, just but be sure:

    import quickInteraction.*;
    Button b = new Button();
    QuickInteraction qi = new QuickInteraction(this);
    

    As expected it gave a "The class Button does not exist" and the "The constructor QuickInteraction(PApplet) is not visible".

    This is the only class in the library:

    package quickInteraction;
    
    import processing.core.PApplet;
    
    public class QuickInteraction {
    
             public static final byte RECT = 0;
             // Lots of public static final bytes used as library constants
             public static final byte PROCENT = 14;
    
             public static PApplet parent;
    
             public QuickInteraction(PApplet parent) {
                  QuickInteraction.parent = parent;
             }
    }
    
  • edited November 2016

    So, I removed all other classes from the .jar file than the quickInteraction.class.

    Java is case sensitive so I would have expected it to be QuickInteraction.class

    Also I assume you are building the library in Eclipse rather than just copying the class file into the sketch?

  • (hint: for posting blocks of code, highlight them and hit ctrl-o. don't rely on the backticks)

  • public static PApplet parent;

    Field parent shouldn't be static! Sometimes folks go w/ multi-windows sketches. :ar!

  • edited November 2016

    quark, it was a typo with the lower case letter, have fixed it in the previous comment. And yes, I am coding it in eclipse, and exporting the jar file from there.

    koogs, did not know, thanks.

    GoToLoop, the rest of the library depends on it being static, but I do find the multiple window aspect appealing. I'll change it once I find a good way to do so.

  • edited November 2016
    • Fix is just as easy as remove keyword static from field parent.
    • Unless you are indeed accessing parent as QuickInteraction.parent throughout your code.
    • If so, you're gonna need to replace QuickInteraction by an instance variable of it: qi.parent.
    • BtW, I find parent's too big for something that's gonna be used a lot.
    • I'd simply call it p or pa instead. :\">
    • Ah! I'd also declare it as final. >-)
  • I agree having the parent as static is not needed but if you remove it you need to modify the constructor :-</p>

    package quickInteraction;
    
    import processing.core.PApplet;
    
    public class QuickInteraction {
    
             public static final byte RECT = 0;
             // Lots of public static final bytes used as library constants
             public static final byte PROCENT = 14;
    
             public PApplet parent;
    
             public QuickInteraction(PApplet parent) {
                  this.parent = parent;
             }
    }
    

    As to replacing parent with p that is a matter of choice, although it saves typing having more descriptive variable names helps debugging large code bases and Eclipse does have auto complete. :)

    Having said all that I can see no reason why your sketch cannot find the constructor provided QuickInteraction is a top-level class i.e. defined in its own file 'QuickInteraction.java'

  • I am exactly accessing it as QuickInteraction.parent throughout the code so that every time for example a new button is made, it does not need it as a parameter.

    I would also have wanted it as a final but I as far I know finals have to be given a value immediately, and as such, I would not be able to set it in the constructor. Is this wrong?

  • But on the topic of it not working, could it be something other than the jar file? If I remember correctly in C++ you need a header file to define what methods you have in the library. But to my knowledge something like that is not needed in java nor processing, right?

  • edited November 2016

    ... as far I know finals have to be given a value immediately, and as such, I would not be able to set it in the constructor. Is this wrong?

    It is wrong... :-\" Field initialization can be delayed till class' constructor. \m/
    And for local variables, regardless they're final or not, they can be delayed just before they're 1st accessed. ~O)

  • And just to cover that front too, I just write the file structure:

    quickInteraction/
        library/
            quickInteraction.jar
        src/
            quickInteraction/
                QuickInteraction.java
        library.properties
        readme.txt
    

    And inside the .jar:

    quickInteraction.jar/
        META-INF/
            MANIFEST.MF
        quickInteraction/
            QuickInteraction.class
    
  • There are no header files in Java or Processing.

    What is the name of the file (???.java) that has the code for the QuickInteraction class?

  • edited November 2016

    File structure shows class folder does not match Java file name - q vs Q again. Seems like a recurring problem to check for. Methods are lowerCamelCase, classes are TitleCase -- and files match the classes they contain.

  • edited November 2016

    What is the name of the file (???.java) that has the code for the QuickInteraction class?

    It is QuickInteraction.java

    I have also renamed all files and folders called quickInteractionto QuickInteraction just to be sure.

    The constructor, however, are not visible.

  • edited November 2016

    The "quickInteraction.jar" is still all lowerCamelCase! Rename it to: "QuickInteraction.jar".
    Try to recompile everything as well. And make sure the final result is: "QuickInteraction.jar".

  • QuickInteraction qi = new QuickInteraction(this);

    Are you executing this statement inside the main sketch code or from inside another class?

  • Inside the sketch.

  • Answer ✓

    Create a new sketch and try and instantiate the QuickIteration object and if it doesn't work post the sketch code and the error message here

  • I tried to recompile it again this morning and now it works. I think it might have been a mix of a non-public constructor, letter casing and perhaps that I renamed the .jar to upper case by hand, not by eclipse.

    Thank you all for your help :)

Sign In or Register to comment.