Issue with custom created plugin

plugins

#1

Hi,
Am a newbie in nativescript development and blocked in middle of custom module creation,
am trying to include a pure android plugin (Simple toast) to my nativescript(typescript) project. For that first i created a nativescript plugin which contains a jar that created using android studio. The plugin works fine without any issue in nativescript+javascript, the real issue comes when i try to import that into my nativescript+typescript project. I got the error when i try to call the function that i newly imported.
Thanks in advance.

Error shown in console : ERROR Error: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resources android.content.Context.getResources()’ on a null object reference

I followed this tutorial to achieve the target
https://www.nativescript.org/blog/plugins-and-jars

Code that i have tried

index.js (Plugin)

var application = require(“application”);
var context = application.android.context;

module.exports = {
showToast: function() {
var toaster = new org.nativescript.kstanoev.toaster.Toaster();
toaster.show(context);
}
};

company.ts
var toaster = require(“nativescript-toaster”);
ngOnInit(){
toaster.showToast();
}


#2

What does org.nativescript.kstanoev.toaster.Toaster's code look like, and which line does the error occur on?


#3

package org.nativescript.kstanoev.toaster;

import android.content.Context;
import android.widget.Toast;

public class Toaster {
public void show(Context context) {
CharSequence text = “Hello NativeScript!”;
int duration = Toast.LENGTH_SHORT;

   Toast toast = Toast.makeText(context, text, duration);
   toast.show();

}
}

ERROR Error: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resources android.content.Context.getResources()’ on a null object reference


#4

It would appear that the context you pass is null. Try debugging the problem with chrome devtools.


#5

Many thanks for the response, but as i said its working on nativescript+js project, problem is with nativescript+typescript. I tried the same plugin for both of those.