Here is the code so far I am trying but it is showing me error:
URL url = null;
try {
url = new URL("http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("1");
Document doc = null;
try {
System.out.println("2");
doc = Jsoup.parse(url, 3000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("3");
Element table = doc.select("table[title=Avgångar:]").first();
System.out.println("4");
Iterator<Element> it = table.select("td").iterator();
//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
// do what ever you want with the td element here
System.out.println(it.next());
//iterate three times to get to the next td you want. checking after the first
// one to make sure
// we're not at the end of the table.
it.next();
if(!it.hasNext()){
break;
}
it.next();
it.next();
}
It prints System.out.println("3");
then it stops in this line
Element table = doc.select("table[title=Avgångar:]").first();
How can i solve this problem,
Thanks
It looks like the website you're trying to parse the HTML from has an error and doesn't have any tables on it. This is what's causing the null pointer exception. doc.select("table[title=Avgångar:]") isn't returning an element and then you're trying to call a method on it. To prevent this error from happening again, you could do something like this:
Elements foundTables = doc.select("table[title=Avgångar:]");
Element table = null;
if(!foundTables.isEmpty()){
table = tables.first();
}
Now, if any table was found, the table variable won't be null. You'll just have to alter the code to adapt in case no tables are found.
You're not checking the result of doc.select() before calling .first(). If there are no elements in the document that match the specified query, doc.select() could return null. Then you are calling .first() on a null pointer which, of course, will throw an exception. There is no table tag with the title you have specified in the document that you are using in your example. So, the result is not surprising.
Related
I am working on parse.com demo, I want to update an existing entry from a table in parse.com. Each time it makes a duplicate entry. I have tried what's below.
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("MQOD");
// query.orderByDescending("createdAt");
query.whereEqualTo("DeviceId", android_id);
// query.toString();
System.out.println("::::::::::::::::::QUERY:::::::::::::::"
+ query);
try {
ob = query.find();
} catch (com.parse.ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
testObject.put("DeviceId", android_id);
testObject.put("InstallationDate", subscriptionDate);
testObject.put("Subscription", "Done");
testObject.put("NextSubscription", upGradeDate);
try {
testObject.save();
} catch (com.parse.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// Purchase error...!!
System.out.println("::::::::::::::::::::::::Purchase consumehas failed:::::::::::::::::::::");
}
}
From your code seems that you don't use the standard field id objectID( that is the official id field used in all the Parse class table ) but the DeviceId ( i suppose it is your custom field ) .
Otherwise you have to make your DeviceId as unique field, and to make it possible, you have to check the new incoming DeviceId in the "beforeSave" parse trigger, and so check if the element already exist or not ( see also this: https://www.parse.com/questions/unique-fields--2 )
So, at now it's normal that you obtain a dubplicate each time, because it is considered as Insert instead of Update ( since you don't specify the objectId )
Hope it helps
I may be misunderstanding what String.contains does. I am now trying to pull a specific link using Jsoup in Android. I'm trying to just get the faceBook one as an example. Ive tried a few things. this one It Seems to be outputting got it on the ones that do not contain the facebook url and leaving the facebook ones blank. How do I just get the FaceBook ones and stop the loop.
protected Void doInBackground(Void... params) {
Document doc = null;
try {
doc = Jsoup.connect("http://www.homedepot.com").get();
Elements link = doc.select("a[href]");
String stringLink = null;
for (int i = 0; i < link.size(); i++) {
stringLink = link.toString();
if (stringLink.contains("https://www.facebook.com/")){
System.out.println(stringLink+"got it");
}
else{
//System.out.println(stringLink+"not it");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
The following line is causing the problem:
stringLink = link.toString();
The link variable is a collection of Elements (in this case every link on the page), so by calling link.toString() you're getting the String representation of every single link on the page all at once! That means stringLink will always contain the facebook link!
Change the line to:
stringLink = link.get(i).toString();
This line gets only the link at index i on each iteration and checks whether or not it contains the facebook link.
So far I've tried this example which I just took from this site but it is not working in my case and I don't know why?! Is there anyone who can tell me how can I solve this problem, here is the code: this code shows me NullExceptionPointer
And i tried to print the doc.select() it shows me error :s null excetptionpointer.
URL url = null;
try {
url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc = null;
try {
doc = Jsoup.parse(url, 3000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element table = doc.select("table[class=niftyd]").first();
Iterator<Element> ite = table.select("td[width=65]").iterator();
ite.next(); // first one is image, skip it
System.out.println("Value 1: " + ite.next().text());
System.out.println("Value 2: " + ite.next().text());
System.out.println("Value 3: " + ite.next().text());
System.out.println("Value 4: " + ite.next().text());
Any help I would really appreciate it, I'm fighting for the whole day and I cant solve it! :s
The problem is doc.select("table[class=niftyd]") returns null. When your call first on it you get the null pointer exception. You're trying to call a method on an object that is actually null. You need make sure what doc.select() returns isn't null.
I keep getting this error
NullPointer
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): Caused by: java.lang.NullPointerException
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): at com.fttech.htmlParser.releaseInfo.onCreate(releaseInfo.java:62)
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-16 22:55:46.360: ERROR/AndroidRuntime(11047): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
Its pointing to my Element here
Element paragraph = overview.select("p").last();
i am using this to retrieve the article
try {
doc = Jsoup.connect(url).get();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(doc == null){
Toast.makeText(this, "Couldnt retrieve game info", Toast.LENGTH_SHORT);
}
else{
// Get the overview div
Element overview = doc.select("div#object-overview").last();
Everytime you look for an element with select("") your calling last() in a chain which assumes it will always find atleast 1 element, in the situation that there is no say "p" in the document, that is when you will encounter a crash.
It's just simple NullPointerExceptions, you need to learn to code defensively:
// If you believe overview could be null
if(overview != null){
ArrayList<Element> paragraphs = overview.select("p"); // Whatever type select(String) returns
Element lastParagraph = null;
if(paragraphs != null){
lastParagraph = paragraphs.last();
} else {
// Deal with not finding "p" (lastParagraph is null in this situation)
}
// Continue with lastParagraph
} else {
// Deal with overview being null
}
Number 1 Java Error (scroll down)
Also you shouldn't really wrap your code with a catch all Exception, try to catch each exception and deal with them individually.
Lookup the API for your get() method Jsoup get() (eclipse tells you this anyway) It throws IOException, so you should just catch this.
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
Log.e("Tag", "Jsoup get didn't get a document", e);
}
Number 5 Java Error (scroll down)
So All I'm trying to do is create a dynamic expandableListView Currently It works if I just do the groupViews. The problem comes in when I have to populate the children of those groupViews.. I don't know if I'm doing something wrong, or if theres another better way to do it. If anyone knows please let me know. I'm open to anything.
Currently I'm pulling my data off a server and the error I'm getting is java null pointer exception. So I'm thinking it might have something to do with how big I specified my array sizes?
private static String[][] children = new String[7][4];
private static String[] groups = new String[7];
Here is the rest of the code when I try to populate the View.
public void getData(){
try {
int tempGroupCount = 0;
URL food_url = new URL (Constants.SERVER_DINING);
BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
temp = my_buffer.readLine();
// prime read
while (temp != null ){
childrenCount = 0;
// check to see if readline equals Location
//Log.w("HERasdfsafdsafdsafE", temp);
// start a new location
if (temp.equalsIgnoreCase("Location"))
{
temp = my_buffer.readLine();
groups[tempGroupCount] = temp;
tempGroupCount++;
Log.w("HERE IS TEMP", temp);
}
temp = my_buffer.readLine();
while (temp.equalsIgnoreCase("Location") == false){
Log.w("ONMG HEHREHRHERHER", temp);
children[groupCount][childrenCount] = "IAJHSDSAD";
childrenCount++;
temp = my_buffer.readLine();
}
groupCount++;
}
my_buffer.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
}
}
to me it looks like an error in the loop - as you are reading another line without checking is it null
your while loop should look something like this methinks:
// prime read
while (temp != null ){
int childrenCount = 0;
// check to see if readline equals Location
// start a new location
//Log.w("HERasdfsafdsafdsafE", temp);
if (temp.equalsIgnoreCase("Location"))
{
temp = my_buffer.readLine();
groups[tempGroupCount] = temp;
tempGroupCount++;
Log.w("HERE IS TEMP", temp);
}
//>>remove following line as that one isn't checked and
//>>you are loosing on a line that is potentialy a child
//temp = my_buffer.readLine();
//>>check do you have first item to add subitems
else if (tempGroupCount>0){
while (temp.equalsIgnoreCase("Location") == false){
Log.w("ONMG HEHREHRHERHER", temp);
children[tempGroupCount-1][childrenCount] = "IAJHSDSAD";
childrenCount++;
temp = my_buffer.readLine();
}
//>>next counter is probably not need but can't see if you're using it somewhere else
//groupCount++;
}
I would first replace strings array to some 2d collection for example arraylist2d ( you can google it ) so you could easally add and remove data from list. If you created adapter that extends BaseExpandableListAdapter everything should be handled without any problems.
About NULLPointer, could you paste stacktrace or more info on which line it occurs ?