Plugin strange error

Hi, i made a plugin following patricks tutorial and im getting a weird behaviour.
the first time i call a method from my plugin i get a blank error on the screen (a dialog that sais nothing) and i get this in developer

null
 > java.lang.NullPointerException

i first thought of some code error on my method but i wasnt able to find such error so i added another method to my plugin that does nothing heres the code of it

public void js_llamado(){

    }

So i thought that if the error was in the code i wouldnt get the blank error but i still get it the very first time i call ANY of the plugins methods, after that first error the following calls work perfecty. So im a bit confused here, what could this be? any ideas?

im using servoy 5.1.2
thanks

If you call methodA the first time, does it do that also for methodB that you call the first time after that, or is it only once for the first method called?

It could be related to a failure of the IClientPlugin instantiation of the IScriptObject the first time, perhaps you are calling methods IClientPluginAccess in the getScriptObject() method of your IClientPlugin implementation without checking that your IClientPluginAccess variable is not null (which can happen if the plugin hasn’t received a call to initialize() beforehand), but it’s hard to tell without seeing your code.

If i call any of the methods, the first time i get the error, after the error it doesnt matter wich one i call, it works fine.

look heres my first class code

public class Watermark implements IClientPlugin {

    private WatermarkProvider provider;
  
    @Override
    public Icon getImage() {
       return null;
    }

    @Override
    public String getName() {
        return "Watermark";
    }

    @Override
    public PreferencePanel[] getPreferencePanels() {
        return null;
    }

    @Override
    public IScriptObject getScriptObject() {
       return new WatermarkProvider();
    }

    @Override
    public void initialize(IClientPluginAccess arg0) throws PluginException {
        //ignore
    }

   @Override
    public Properties getProperties() {
        Properties props = new Properties();
        props.put(DISPLAY_NAME, getName());
        return props;
    }

    @Override
    public void load() throws PluginException {
        // ignore

    }

    @Override
    public void unload() throws PluginException {
        provider = null;
    }

     @Override
    public void propertyChange(PropertyChangeEvent arg0) {
        // ignore

    }

}

in the other one i implement the IScriptObject and have the 2 methods the empty one and the other along with the ones from the interface, if u think the problem might be there i can show those too

What good is your provider variable to then?
You never set it to anything, still you don’t forget to set it to null in the unload() method. :lol:

You should alter your getScriptObject() method like this:

public IScriptObject getScriptObject() {
    if (provider == null) {
        provider = new WatermarkProvider();
    }
    return provider;
}

Maybe it will fix your problem, because right now you are creating a new instance of your script object for each call of the getScriptObject() method, so it might be (depending what you also have in your WatermarkProvider()) that the first time the method is called to an instance that has already been disposed of by Servoy: so null

Hope this helps,

thats how it was originally, i thought that perhaps without using the provider i could get lucky but the results were the same.
Any way i returned it to how u say using the provider but no good.

look heres my watermarProvider code

public class WatermarkProvider implements IScriptObject {
     
    public void js_llamado(){

    }
    
           public void js_markImage(String srcImg, String markImg, float alpha, int mark_position)
      {
          int OFFSET_X = 10;
          int OFFSET_Y = 10;

        try
        {
          File _file = new File(srcImg);
          if(!_file.exists()) return;
          
          Image src = ImageIO.read(_file);
          int width = src.getWidth(null);
          int height = src.getHeight(null);
          BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
          Graphics2D g = image.createGraphics();
          g.drawImage(src, 0, 0, width, height, null);

          // watermark image file
          File markFile = new File(markImg);
          if(!markFile.exists()) return;
          
          Image mark_img = ImageIO.read(markFile);
          int mark_img_width = mark_img.getWidth(null);
          int mark_img_height = mark_img.getHeight(null);
          g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
          switch (mark_position)
          {
          case 1://WatermarkProvider.MARK_LEFT_TOP:
            g.drawImage(mark_img, OFFSET_X, OFFSET_Y, mark_img_width, mark_img_height, null);
            break;
          case 2://WatermarkProvider.MARK_LEFT_BOTTOM:
            g.drawImage(mark_img, OFFSET_X, (height - mark_img_height - OFFSET_Y), mark_img_width, mark_img_height, null);
            break;
          case 3://WatermarkProvider.MARK_CENTER:
            g.drawImage(mark_img, (width - mark_img_width - OFFSET_X) / 2, (height - mark_img_height - OFFSET_Y) / 2,
                mark_img_width, mark_img_height, null);
            break;
          case 4://WatermarkProvider.MARK_RIGHT_TOP:
            g.drawImage(mark_img, (width - mark_img_width - OFFSET_X), OFFSET_Y, mark_img_width, mark_img_height, null);
            break;
          case 5://WatermarkProvider.MARK_RIGHT_BOTTOM:
          default:
            g.drawImage(mark_img, (width - mark_img_width - OFFSET_X), (height - mark_img_height - OFFSET_Y),
                mark_img_width, mark_img_height, null);
          }

          g.dispose();
          FileOutputStream out = new FileOutputStream(srcImg);
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          encoder.encode(image);
          out.close();
        } catch (Exception e)
        {
          e.printStackTrace();
        }
      }

      @Override
    public String[] getParameterNames(String arg0) {
        // TODO Auto-generated method stub
        String[] ar = new String[] {"hola"};
        
        return null;
    }

    @Override
    public String getSample(String arg0) {
        // TODO Auto-generated method stub
        return "ejemplo";
    }

    
    @Override
    public String getToolTip(String arg0) {
        // TODO Auto-generated method stub
        return "tool tip";
    }

    
    @Override
    public boolean isDeprecated(String arg0) {
        // TODO Auto-generated method stub
        return false;
    }

   
    @Override
    public Class<?>[] getAllReturnedTypes() {
        // TODO Auto-generated method stub
        return new Class[] {null};
        //return null;
    }

}

and i dont believe the error is in the mark_image code cause i get it calling the other method also so i really have no idea

Ah! I believe your problem is here:

    public Class<?>[] getAllReturnedTypes() {
        // TODO Auto-generated method stub
        return new Class[] {null};
        //return null;
    }

Right now you are declaring an array that returns one null object, and Servoy is trying to get a handle on it.

So it should work if you change it to:

    public Class[] getAllReturnedTypes() {
        return null;
    }

And while you’re at it, if you want your plugin to work with Java 5, set the compiler target to java 5,
then you will also have to get rid of all the @Override on interface methods (Java 5 doesn’t allow that annotation on interface implementation, only real method overrides)

Hope this helps,

Thanks a lot once more patrick! haha that solved it :D