Cannot launch an activity in android using intent [duplicate]
up vote
-2
down vote
favorite
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have 2 modules in my application as follow app and interface. interface was an imported aar file.
I am trying to launch an activity in the interface module but every time the application crashes. Launching activities on the app module works fine
I had tried the other solutions available in this forum but was in vain. Find the codes below
QuestionAnalyser class
package com.example.dell.bubblebot;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.dell.yandexapilanguagetranslator.Translator;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class QuestionAnalyser extends AppCompatActivity {
public static String question;
public static String words;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
Translator.setKey ("trnsl.1.1.20181013T134643Z.debdb5811d6caba2.3a4daf18b25c9f4b97662d5e9ac45ed81b199c84");
}
public void analyse(String userQues) {
TranslatorServant t = new TranslatorServant();
question = userQues.replaceAll("\p{Punct}|\d", "");
// question = t.translateText(userQues).replaceAll("\p{Punct}|\d", "");
words = question.split(" ");
getEntity(words);
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
check(words);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void mimicOtherMessage(String message) {
ChatMessage chatMessage = new ChatMessage(message, false);
Interface.madapter.add(chatMessage);
}
public void mimicOtherMessage() {
ChatMessage chatMessage = new ChatMessage(null, false);
Interface.madapter.add(chatMessage);
}
public void check(String words) {
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
}
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
}
}
}
if (identified == false) {
mimicOtherMessage("Sorry, I failed to understand your question.");
}
}
private void initialiseEntityServant(String ent, final String userQues, final String words) {
if (ent.equalsIgnoreCase("cuisine")) {
cuisine c=new cuisine();
c.analyse();
}
if(ent.equalsIgnoreCase("basic")){
final List<String> urlsList = new ArrayList<String>();
final List<String> descList = new ArrayList<String>();
urlsList.add("https://media.timeout.com/images/103708802/image.jpg");
urlsList.add(""https://images.myguide-cdn.com/mauritius/companies/eighty-eight-chinese-restaurant/large/eighty-eight-chinese-restaurant-492783.png");
descList.add("A Chinese dish of small steamed or fried savoury dumplings containing various fillings. Definitely worth a try");
descList.add("Traditionally, this Chinese chicken dish is a dry-stir fry — not a lot of sauce is added into Kung Pao chicken. However, with the amount of flavour explosions happening in this dish, you don’t miss OR want any more sauce.");
Intent i= new Intent(getApplicationContext(),gallery.class);
i.putExtra("urls",urlsList.toArray());
i.putExtra("desc",descList.toArray());
startActivity(i);
}
// will need to initialise other classes
}
//add class to check if question and answer already exists
}
In this code i am calling the gallery activity
gallery class:
package com.example.dell.bubblebot;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class gallery extends AppCompatActivity {
public static Stringdesc;
public static TextView descTextbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
descTextbox=(TextView)findViewById(R.id.desc);
ViewPager viewPager=findViewById(R.id.view_pager);
String imageUrls=getIntent().getStringArrayExtra("urls");
String descArr=getIntent().getStringArrayExtra("desc");
desc=descArr;
ViewPagerAdapter adapter= new ViewPagerAdapter(this,imageUrls);
viewPager.setAdapter(adapter);
}
}
Error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samples.chatbotsample1, PID: 14653
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:112)
at com.example.dell.bubblebot.QuestionAnalyser.initialiseEntityServant(QuestionAnalyser.java:126)
at com.example.dell.bubblebot.QuestionAnalyser.check(QuestionAnalyser.java:93)
at com.example.dell.bubblebot.QuestionAnalyser$1.onDataChange(QuestionAnalyser.java:61)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java
marked as duplicate by Kling Klang
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
up vote
-2
down vote
favorite
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have 2 modules in my application as follow app and interface. interface was an imported aar file.
I am trying to launch an activity in the interface module but every time the application crashes. Launching activities on the app module works fine
I had tried the other solutions available in this forum but was in vain. Find the codes below
QuestionAnalyser class
package com.example.dell.bubblebot;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.dell.yandexapilanguagetranslator.Translator;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class QuestionAnalyser extends AppCompatActivity {
public static String question;
public static String words;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
Translator.setKey ("trnsl.1.1.20181013T134643Z.debdb5811d6caba2.3a4daf18b25c9f4b97662d5e9ac45ed81b199c84");
}
public void analyse(String userQues) {
TranslatorServant t = new TranslatorServant();
question = userQues.replaceAll("\p{Punct}|\d", "");
// question = t.translateText(userQues).replaceAll("\p{Punct}|\d", "");
words = question.split(" ");
getEntity(words);
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
check(words);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void mimicOtherMessage(String message) {
ChatMessage chatMessage = new ChatMessage(message, false);
Interface.madapter.add(chatMessage);
}
public void mimicOtherMessage() {
ChatMessage chatMessage = new ChatMessage(null, false);
Interface.madapter.add(chatMessage);
}
public void check(String words) {
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
}
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
}
}
}
if (identified == false) {
mimicOtherMessage("Sorry, I failed to understand your question.");
}
}
private void initialiseEntityServant(String ent, final String userQues, final String words) {
if (ent.equalsIgnoreCase("cuisine")) {
cuisine c=new cuisine();
c.analyse();
}
if(ent.equalsIgnoreCase("basic")){
final List<String> urlsList = new ArrayList<String>();
final List<String> descList = new ArrayList<String>();
urlsList.add("https://media.timeout.com/images/103708802/image.jpg");
urlsList.add(""https://images.myguide-cdn.com/mauritius/companies/eighty-eight-chinese-restaurant/large/eighty-eight-chinese-restaurant-492783.png");
descList.add("A Chinese dish of small steamed or fried savoury dumplings containing various fillings. Definitely worth a try");
descList.add("Traditionally, this Chinese chicken dish is a dry-stir fry — not a lot of sauce is added into Kung Pao chicken. However, with the amount of flavour explosions happening in this dish, you don’t miss OR want any more sauce.");
Intent i= new Intent(getApplicationContext(),gallery.class);
i.putExtra("urls",urlsList.toArray());
i.putExtra("desc",descList.toArray());
startActivity(i);
}
// will need to initialise other classes
}
//add class to check if question and answer already exists
}
In this code i am calling the gallery activity
gallery class:
package com.example.dell.bubblebot;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class gallery extends AppCompatActivity {
public static Stringdesc;
public static TextView descTextbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
descTextbox=(TextView)findViewById(R.id.desc);
ViewPager viewPager=findViewById(R.id.view_pager);
String imageUrls=getIntent().getStringArrayExtra("urls");
String descArr=getIntent().getStringArrayExtra("desc");
desc=descArr;
ViewPagerAdapter adapter= new ViewPagerAdapter(this,imageUrls);
viewPager.setAdapter(adapter);
}
}
Error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samples.chatbotsample1, PID: 14653
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:112)
at com.example.dell.bubblebot.QuestionAnalyser.initialiseEntityServant(QuestionAnalyser.java:126)
at com.example.dell.bubblebot.QuestionAnalyser.check(QuestionAnalyser.java:93)
at com.example.dell.bubblebot.QuestionAnalyser$1.onDataChange(QuestionAnalyser.java:61)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java
marked as duplicate by Kling Klang
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
TheQuestionAnalyserclass appears to extend someContextWrapperclass – e.g.,Activity,Service, etc. – and it likely shouldn't. You cannot instantiate those classes yourself.
– Mike M.
Nov 20 at 8:52
No, the QuestionAnalyser class is extending only AppCompatActivity.
– Samiirah Aujub
Nov 20 at 9:06
Same thing. Are you using it as anActivity? That is, does it actually show on-screen, or is it a utility class? Where and how isinitialiseEntityServant()being called?
– Mike M.
Nov 20 at 9:07
No it does not. This class is only used for processing. Then how should i call an activity within this class?
– Samiirah Aujub
Nov 20 at 9:09
If it doesn't actually show on-screen, it should not extend anyActivityclass or subclass. If you need aContextin that class, or even just in that method, then pass aContextinto it, either via a constructor, or by adding aContextparameter to theinitialiseEntityServant()method.
– Mike M.
Nov 20 at 9:11
|
show 3 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have 2 modules in my application as follow app and interface. interface was an imported aar file.
I am trying to launch an activity in the interface module but every time the application crashes. Launching activities on the app module works fine
I had tried the other solutions available in this forum but was in vain. Find the codes below
QuestionAnalyser class
package com.example.dell.bubblebot;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.dell.yandexapilanguagetranslator.Translator;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class QuestionAnalyser extends AppCompatActivity {
public static String question;
public static String words;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
Translator.setKey ("trnsl.1.1.20181013T134643Z.debdb5811d6caba2.3a4daf18b25c9f4b97662d5e9ac45ed81b199c84");
}
public void analyse(String userQues) {
TranslatorServant t = new TranslatorServant();
question = userQues.replaceAll("\p{Punct}|\d", "");
// question = t.translateText(userQues).replaceAll("\p{Punct}|\d", "");
words = question.split(" ");
getEntity(words);
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
check(words);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void mimicOtherMessage(String message) {
ChatMessage chatMessage = new ChatMessage(message, false);
Interface.madapter.add(chatMessage);
}
public void mimicOtherMessage() {
ChatMessage chatMessage = new ChatMessage(null, false);
Interface.madapter.add(chatMessage);
}
public void check(String words) {
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
}
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
}
}
}
if (identified == false) {
mimicOtherMessage("Sorry, I failed to understand your question.");
}
}
private void initialiseEntityServant(String ent, final String userQues, final String words) {
if (ent.equalsIgnoreCase("cuisine")) {
cuisine c=new cuisine();
c.analyse();
}
if(ent.equalsIgnoreCase("basic")){
final List<String> urlsList = new ArrayList<String>();
final List<String> descList = new ArrayList<String>();
urlsList.add("https://media.timeout.com/images/103708802/image.jpg");
urlsList.add(""https://images.myguide-cdn.com/mauritius/companies/eighty-eight-chinese-restaurant/large/eighty-eight-chinese-restaurant-492783.png");
descList.add("A Chinese dish of small steamed or fried savoury dumplings containing various fillings. Definitely worth a try");
descList.add("Traditionally, this Chinese chicken dish is a dry-stir fry — not a lot of sauce is added into Kung Pao chicken. However, with the amount of flavour explosions happening in this dish, you don’t miss OR want any more sauce.");
Intent i= new Intent(getApplicationContext(),gallery.class);
i.putExtra("urls",urlsList.toArray());
i.putExtra("desc",descList.toArray());
startActivity(i);
}
// will need to initialise other classes
}
//add class to check if question and answer already exists
}
In this code i am calling the gallery activity
gallery class:
package com.example.dell.bubblebot;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class gallery extends AppCompatActivity {
public static Stringdesc;
public static TextView descTextbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
descTextbox=(TextView)findViewById(R.id.desc);
ViewPager viewPager=findViewById(R.id.view_pager);
String imageUrls=getIntent().getStringArrayExtra("urls");
String descArr=getIntent().getStringArrayExtra("desc");
desc=descArr;
ViewPagerAdapter adapter= new ViewPagerAdapter(this,imageUrls);
viewPager.setAdapter(adapter);
}
}
Error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samples.chatbotsample1, PID: 14653
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:112)
at com.example.dell.bubblebot.QuestionAnalyser.initialiseEntityServant(QuestionAnalyser.java:126)
at com.example.dell.bubblebot.QuestionAnalyser.check(QuestionAnalyser.java:93)
at com.example.dell.bubblebot.QuestionAnalyser$1.onDataChange(QuestionAnalyser.java:61)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I have 2 modules in my application as follow app and interface. interface was an imported aar file.
I am trying to launch an activity in the interface module but every time the application crashes. Launching activities on the app module works fine
I had tried the other solutions available in this forum but was in vain. Find the codes below
QuestionAnalyser class
package com.example.dell.bubblebot;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.example.dell.yandexapilanguagetranslator.Translator;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class QuestionAnalyser extends AppCompatActivity {
public static String question;
public static String words;
String entityIdentified;
static boolean identified = false;
DatabaseReference entityRef;
TranslatorServant t = new TranslatorServant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interface);
Translator.setKey ("trnsl.1.1.20181013T134643Z.debdb5811d6caba2.3a4daf18b25c9f4b97662d5e9ac45ed81b199c84");
}
public void analyse(String userQues) {
TranslatorServant t = new TranslatorServant();
question = userQues.replaceAll("\p{Punct}|\d", "");
// question = t.translateText(userQues).replaceAll("\p{Punct}|\d", "");
words = question.split(" ");
getEntity(words);
}
private void getEntity(final String words) {
entityRef = FirebaseDatabase.getInstance().getReference().child("IRAdata").child("Entities");
entityRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot entity : dataSnapshot.getChildren()) {
String synonyms = entity.child("syn").getValue().toString().split(",");
for (String syn : synonyms) {
for (String word : words) {
if (word.equalsIgnoreCase(syn) || question.equalsIgnoreCase(syn)) {
entityIdentified = entity.getKey();
identified = true;
}
}
}
}
check(words);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void mimicOtherMessage(String message) {
ChatMessage chatMessage = new ChatMessage(message, false);
Interface.madapter.add(chatMessage);
}
public void mimicOtherMessage() {
ChatMessage chatMessage = new ChatMessage(null, false);
Interface.madapter.add(chatMessage);
}
public void check(String words) {
if (identified == true) {
initialiseEntityServant(entityIdentified, question, words);
}
if (identified == false) {
for (String yes : YesNo.yes) {
for (String w : words) {
if (w.equalsIgnoreCase(yes) || question.equalsIgnoreCase(yes)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
for (String no : YesNo.no) {
for (String w : words) {
if (w.equalsIgnoreCase(no) || question.equalsIgnoreCase(no)) {
identified = true;
entityIdentified = "basic";
initialiseEntityServant(entityIdentified, question, words);
}
}
}
}
}
if (identified == false) {
mimicOtherMessage("Sorry, I failed to understand your question.");
}
}
private void initialiseEntityServant(String ent, final String userQues, final String words) {
if (ent.equalsIgnoreCase("cuisine")) {
cuisine c=new cuisine();
c.analyse();
}
if(ent.equalsIgnoreCase("basic")){
final List<String> urlsList = new ArrayList<String>();
final List<String> descList = new ArrayList<String>();
urlsList.add("https://media.timeout.com/images/103708802/image.jpg");
urlsList.add(""https://images.myguide-cdn.com/mauritius/companies/eighty-eight-chinese-restaurant/large/eighty-eight-chinese-restaurant-492783.png");
descList.add("A Chinese dish of small steamed or fried savoury dumplings containing various fillings. Definitely worth a try");
descList.add("Traditionally, this Chinese chicken dish is a dry-stir fry — not a lot of sauce is added into Kung Pao chicken. However, with the amount of flavour explosions happening in this dish, you don’t miss OR want any more sauce.");
Intent i= new Intent(getApplicationContext(),gallery.class);
i.putExtra("urls",urlsList.toArray());
i.putExtra("desc",descList.toArray());
startActivity(i);
}
// will need to initialise other classes
}
//add class to check if question and answer already exists
}
In this code i am calling the gallery activity
gallery class:
package com.example.dell.bubblebot;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class gallery extends AppCompatActivity {
public static Stringdesc;
public static TextView descTextbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
descTextbox=(TextView)findViewById(R.id.desc);
ViewPager viewPager=findViewById(R.id.view_pager);
String imageUrls=getIntent().getStringArrayExtra("urls");
String descArr=getIntent().getStringArrayExtra("desc");
desc=descArr;
ViewPagerAdapter adapter= new ViewPagerAdapter(this,imageUrls);
viewPager.setAdapter(adapter);
}
}
Error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samples.chatbotsample1, PID: 14653
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:112)
at com.example.dell.bubblebot.QuestionAnalyser.initialiseEntityServant(QuestionAnalyser.java:126)
at com.example.dell.bubblebot.QuestionAnalyser.check(QuestionAnalyser.java:93)
at com.example.dell.bubblebot.QuestionAnalyser$1.onDataChange(QuestionAnalyser.java:61)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
java
java
edited Nov 20 at 10:30
Kling Klang
32.2k156287
32.2k156287
asked Nov 20 at 8:46
Samiirah Aujub
45
45
marked as duplicate by Kling Klang
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Kling Klang
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
TheQuestionAnalyserclass appears to extend someContextWrapperclass – e.g.,Activity,Service, etc. – and it likely shouldn't. You cannot instantiate those classes yourself.
– Mike M.
Nov 20 at 8:52
No, the QuestionAnalyser class is extending only AppCompatActivity.
– Samiirah Aujub
Nov 20 at 9:06
Same thing. Are you using it as anActivity? That is, does it actually show on-screen, or is it a utility class? Where and how isinitialiseEntityServant()being called?
– Mike M.
Nov 20 at 9:07
No it does not. This class is only used for processing. Then how should i call an activity within this class?
– Samiirah Aujub
Nov 20 at 9:09
If it doesn't actually show on-screen, it should not extend anyActivityclass or subclass. If you need aContextin that class, or even just in that method, then pass aContextinto it, either via a constructor, or by adding aContextparameter to theinitialiseEntityServant()method.
– Mike M.
Nov 20 at 9:11
|
show 3 more comments
TheQuestionAnalyserclass appears to extend someContextWrapperclass – e.g.,Activity,Service, etc. – and it likely shouldn't. You cannot instantiate those classes yourself.
– Mike M.
Nov 20 at 8:52
No, the QuestionAnalyser class is extending only AppCompatActivity.
– Samiirah Aujub
Nov 20 at 9:06
Same thing. Are you using it as anActivity? That is, does it actually show on-screen, or is it a utility class? Where and how isinitialiseEntityServant()being called?
– Mike M.
Nov 20 at 9:07
No it does not. This class is only used for processing. Then how should i call an activity within this class?
– Samiirah Aujub
Nov 20 at 9:09
If it doesn't actually show on-screen, it should not extend anyActivityclass or subclass. If you need aContextin that class, or even just in that method, then pass aContextinto it, either via a constructor, or by adding aContextparameter to theinitialiseEntityServant()method.
– Mike M.
Nov 20 at 9:11
The
QuestionAnalyser class appears to extend some ContextWrapper class – e.g., Activity, Service, etc. – and it likely shouldn't. You cannot instantiate those classes yourself.– Mike M.
Nov 20 at 8:52
The
QuestionAnalyser class appears to extend some ContextWrapper class – e.g., Activity, Service, etc. – and it likely shouldn't. You cannot instantiate those classes yourself.– Mike M.
Nov 20 at 8:52
No, the QuestionAnalyser class is extending only AppCompatActivity.
– Samiirah Aujub
Nov 20 at 9:06
No, the QuestionAnalyser class is extending only AppCompatActivity.
– Samiirah Aujub
Nov 20 at 9:06
Same thing. Are you using it as an
Activity? That is, does it actually show on-screen, or is it a utility class? Where and how is initialiseEntityServant() being called?– Mike M.
Nov 20 at 9:07
Same thing. Are you using it as an
Activity? That is, does it actually show on-screen, or is it a utility class? Where and how is initialiseEntityServant() being called?– Mike M.
Nov 20 at 9:07
No it does not. This class is only used for processing. Then how should i call an activity within this class?
– Samiirah Aujub
Nov 20 at 9:09
No it does not. This class is only used for processing. Then how should i call an activity within this class?
– Samiirah Aujub
Nov 20 at 9:09
If it doesn't actually show on-screen, it should not extend any
Activity class or subclass. If you need a Context in that class, or even just in that method, then pass a Context into it, either via a constructor, or by adding a Context parameter to the initialiseEntityServant() method.– Mike M.
Nov 20 at 9:11
If it doesn't actually show on-screen, it should not extend any
Activity class or subclass. If you need a Context in that class, or even just in that method, then pass a Context into it, either via a constructor, or by adding a Context parameter to the initialiseEntityServant() method.– Mike M.
Nov 20 at 9:11
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
Notice:
Class A { ...
}
getApplicationContext belongs to abstract class Context, if you want to use this method in your custom class, the class should extends Context or any of its sub class, such as Application,Activity, etc.
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Notice:
Class A { ...
}
getApplicationContext belongs to abstract class Context, if you want to use this method in your custom class, the class should extends Context or any of its sub class, such as Application,Activity, etc.
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
add a comment |
up vote
0
down vote
Notice:
Class A { ...
}
getApplicationContext belongs to abstract class Context, if you want to use this method in your custom class, the class should extends Context or any of its sub class, such as Application,Activity, etc.
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
add a comment |
up vote
0
down vote
up vote
0
down vote
Notice:
Class A { ...
}
getApplicationContext belongs to abstract class Context, if you want to use this method in your custom class, the class should extends Context or any of its sub class, such as Application,Activity, etc.
Notice:
Class A { ...
}
getApplicationContext belongs to abstract class Context, if you want to use this method in your custom class, the class should extends Context or any of its sub class, such as Application,Activity, etc.
answered Nov 20 at 9:04
navylover
3,13921118
3,13921118
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
add a comment |
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
can you please illustrate an example how can i do that?
– Samiirah Aujub
Nov 20 at 9:07
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
@SamiirahAujub the key is pass a context to the A class, there are many ways.
– navylover
Nov 20 at 9:13
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
I have tried passing a context from a previous class to class A(QuestionAnalyser) but instead i get an error of android.app.ActivityThread.getApplicationThread()
– Samiirah Aujub
Nov 20 at 9:17
add a comment |
The
QuestionAnalyserclass appears to extend someContextWrapperclass – e.g.,Activity,Service, etc. – and it likely shouldn't. You cannot instantiate those classes yourself.– Mike M.
Nov 20 at 8:52
No, the QuestionAnalyser class is extending only AppCompatActivity.
– Samiirah Aujub
Nov 20 at 9:06
Same thing. Are you using it as an
Activity? That is, does it actually show on-screen, or is it a utility class? Where and how isinitialiseEntityServant()being called?– Mike M.
Nov 20 at 9:07
No it does not. This class is only used for processing. Then how should i call an activity within this class?
– Samiirah Aujub
Nov 20 at 9:09
If it doesn't actually show on-screen, it should not extend any
Activityclass or subclass. If you need aContextin that class, or even just in that method, then pass aContextinto it, either via a constructor, or by adding aContextparameter to theinitialiseEntityServant()method.– Mike M.
Nov 20 at 9:11