使用数组列表提取数据时,尝试在空对象引用上调用虚方法 'java.lang.String'
Attempt to invoke virtual method 'java.lang.String' on a null object reference when extracting data using from an arraylist
我是这个平台的新手,一段时间以来我一直被这个错误所困,我在网上搜索过这个错误,但似乎没有一个答案能满足我的情况。我正在尝试使用 Android 上的 XmlPullparser 提取数据,并将提取的数据添加到模型类型的数组列表(我定义的 'model' 对象)。 XmlPullparser 正在正确解析文档,因为我试图将数据存储在 String 类型的数组列表中并且它起作用了。当我存储到前者并出现以下错误时出现问题
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.newsmodel.model.getTitles()' on a null object reference
at com.example.newsmodel.MainActivity$GetStoriesInBackground.onPostExecute(MainActivity.java:121)
at com.example.newsmodel.MainActivity$GetStoriesInBackground.onPostExecute(MainActivity.java:49)
at android.os.AsyncTask.finish(AsyncTask.java:755)
at android.os.AsyncTask.access0(AsyncTask.java:192)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
at android.os.Handler.dispatchMessage(Handler.java:107)
下面是解析 Xml 文档的代码:
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView tvtitle;
TextView tvDescription;
ArrayList<String> titles;
ArrayList<String> descriptions;
ArrayList<model> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvtitle = (TextView) findViewById(R.id.tvTitle);
tvDescription = (TextView) findViewById(R.id.tvDescription);
titles = new ArrayList<>();
descriptions = new ArrayList<>();
items = new ArrayList<model>();
new GetStoriesInBackground().execute();
}
private InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
@SuppressLint("StaticFieldLeak")
public class GetStoriesInBackground extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL("https://www.businessdailyafrica.com/539444-539444-view-asFeed-bfdflfz/index.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
//variable to check if parser is inside <item> tag
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
model item = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
item = new model();
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
//titles.add(xpp.nextText()); //extract the headline
item.setTitles(xpp.nextText());
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem)
//descriptions.add(xpp.nextText());//extract the link of article
item.setDescriptions(xpp.nextText());
//add item to the arraylist after the last required tag has been extracted
items.add(item);
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// tvtitle.setText(titles.get(1));
// tvDescription.setText(descriptions.get(1));
tvtitle.setText(items.get(0).getTitles());
tvDescription.setText(items.get(0).getDescriptions());
}
}
}
对象class:
public class model {
public String titles;
public String descriptions;
public String getTitles() {
return titles;
}
public void setTitles(String titles) {
this.titles = titles;
}
public String getDescriptions() {
return descriptions;
}
public void setDescriptions(String descriptions) {
this.descriptions = descriptions;
}
}
证明解析器在使用 descriptions/titles Arraylist 时正在提取数据:
emulator screenshot with text extracted from xml
找到了我的问题的解决方案...我只是将 'item=new model()' 从初始 if 语句 ( 'if (xpp.getName().equalsIgnoreCase("item"))') 移到顶部但在 while loop.The 新代码中块现在是这样的:
URL url = new URL("https://www.businessdailyafrica.com/539444-539444-view-asFeed-bfdflfz/index.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
String Title="";
String Description = "";
while (eventType != XmlPullParser.END_DOCUMENT) {
model item = new model();
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
Title= xpp.nextText();
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem)
Description = xpp.nextText();
item.setDescriptions(Description);
item.setTitles(Title);
//add item to the arraylist after the last required tag has been extracted
items.add(item);
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
我是这个平台的新手,一段时间以来我一直被这个错误所困,我在网上搜索过这个错误,但似乎没有一个答案能满足我的情况。我正在尝试使用 Android 上的 XmlPullparser 提取数据,并将提取的数据添加到模型类型的数组列表(我定义的 'model' 对象)。 XmlPullparser 正在正确解析文档,因为我试图将数据存储在 String 类型的数组列表中并且它起作用了。当我存储到前者并出现以下错误时出现问题
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.newsmodel.model.getTitles()' on a null object reference
at com.example.newsmodel.MainActivity$GetStoriesInBackground.onPostExecute(MainActivity.java:121)
at com.example.newsmodel.MainActivity$GetStoriesInBackground.onPostExecute(MainActivity.java:49)
at android.os.AsyncTask.finish(AsyncTask.java:755)
at android.os.AsyncTask.access0(AsyncTask.java:192)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
at android.os.Handler.dispatchMessage(Handler.java:107)
下面是解析 Xml 文档的代码:
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView tvtitle;
TextView tvDescription;
ArrayList<String> titles;
ArrayList<String> descriptions;
ArrayList<model> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvtitle = (TextView) findViewById(R.id.tvTitle);
tvDescription = (TextView) findViewById(R.id.tvDescription);
titles = new ArrayList<>();
descriptions = new ArrayList<>();
items = new ArrayList<model>();
new GetStoriesInBackground().execute();
}
private InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
@SuppressLint("StaticFieldLeak")
public class GetStoriesInBackground extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL("https://www.businessdailyafrica.com/539444-539444-view-asFeed-bfdflfz/index.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
//variable to check if parser is inside <item> tag
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
model item = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
item = new model();
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
//titles.add(xpp.nextText()); //extract the headline
item.setTitles(xpp.nextText());
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem)
//descriptions.add(xpp.nextText());//extract the link of article
item.setDescriptions(xpp.nextText());
//add item to the arraylist after the last required tag has been extracted
items.add(item);
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// tvtitle.setText(titles.get(1));
// tvDescription.setText(descriptions.get(1));
tvtitle.setText(items.get(0).getTitles());
tvDescription.setText(items.get(0).getDescriptions());
}
}
}
对象class:
public class model {
public String titles;
public String descriptions;
public String getTitles() {
return titles;
}
public void setTitles(String titles) {
this.titles = titles;
}
public String getDescriptions() {
return descriptions;
}
public void setDescriptions(String descriptions) {
this.descriptions = descriptions;
}
}
证明解析器在使用 descriptions/titles Arraylist 时正在提取数据: emulator screenshot with text extracted from xml
找到了我的问题的解决方案...我只是将 'item=new model()' 从初始 if 语句 ( 'if (xpp.getName().equalsIgnoreCase("item"))') 移到顶部但在 while loop.The 新代码中块现在是这样的:
URL url = new URL("https://www.businessdailyafrica.com/539444-539444-view-asFeed-bfdflfz/index.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
String Title="";
String Description = "";
while (eventType != XmlPullParser.END_DOCUMENT) {
model item = new model();
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
Title= xpp.nextText();
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem)
Description = xpp.nextText();
item.setDescriptions(Description);
item.setTitles(Title);
//add item to the arraylist after the last required tag has been extracted
items.add(item);
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}