我的问题是我写了一个登录网站并下载所需数据的程序。一切都很好,程序突然停止工作时工作。在代码中,没有任何变化,即使程序每次都开始抛出异常。什么可能导致这个?我该如何解决这个问题?
例外是:
Exception in thread "main" java.lang.NullPointerException
at gui.JsoupClass.checkLogin(JsoupClass.java:55)
at gui.JsoupClass.<init>(JsoupClass.java:31)
at gui.JsoupClass.main(JsoupClass.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
这是我的代码,也许我看不到你们中的一些人会看到的东西:
package gui;
import dao.*;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import javax.swing.*;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JsoupClass
{
static String value[] = new String[3];
static String name[] = new String[3];
public static void main(String args[]) {
new JsoupClass("email@gmail.com", "yourpassword", null);
}
public JsoupClass(String emailText, String passText, MainFrame mainFrame) {
Connection.Response res;
res = getLoginData();
res = doLogin(res, emailText, passText);
if (checkLogin(res).hasText()) {
setUpTrafficPayment(res);
setUpEwalletData(res);
setUpPanelsData(res);
setUpPaymentData(res);
try {
if (mainFrame != null)
mainFrame.dispose();
new StatisticFrame();
} catch (ParseException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Wrong Login Data!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
}
public Elements checkLogin(Connection.Response res) {
Document doc = null;
Elements elements;
try {
doc = res.parse();
} catch (IOException e) {
e.printStackTrace();
}
elements = doc.select("div#topuserinfo > a > span");
if (elements.hasText()) {
UserData userData = UserData.getInstance();
userData.setNameValue(elements.text());
}
return elements;
}
public Document getPage(Connection.Response res, String pageAddres) {
Document doc = null;
try {
doc = Jsoup
.connect(pageAddres)
.cookie("PHPSESSID", res.cookie("PHPSESSID"))
.get();
} catch (SocketTimeoutException e) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Read timed out! Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
public Connection.Response doLogin(Connection.Response res, String email, String pass) {
Connection.Response res2 = null;
try {
res2 = Jsoup
.connect("https://bannersbroker.com//user/login?event=doLogin")
.cookies(res.cookies())
.data("email", email)
.data("pass", pass)
.data(name[0], value[0])
.data(name[1], value[1])
.data(name[2], value[2])
.method(Connection.Method.POST)
.execute();
} catch (IOException ioe) {
} catch (IllegalArgumentException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
} catch (NullPointerException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
return res2;
}
public Connection.Response getLoginData() {
Connection.Response res = null;
try {
String html;
int length, counter;
res = Jsoup
.connect("https://bannersbroker.com//user/login?event=doLogin")
.execute();
html = res.parse().toString();
length = html.length();
counter = 0;
for (int i = 0; i < length; i++) {
if (html.startsWith("document.write", i)) {
name[counter] = html.substring(i + 41, i + 144);
value[counter] = "Login";
counter++;
}
if (html.startsWith("hidden", i)) {
name[counter] = html.substring(i + 13, i + 81);
value[counter] = html.substring(i + 90, i + 123);
counter++;
}
}
} catch (IOException ioe) {
} catch (NullPointerException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
return res;
}
public void setUpEwalletData(Connection.Response res) {
Document doc;
Elements elements;
doc = getPage(res, "http://bannersbroker.com/adpubcombo_ewallet/ewallet");
elements = doc.select("p[class=ewallet_numbers");
EwalletData ewalletData = EwalletData.getInstance();
ewalletData.setWalletValue(Double.parseDouble(elements.first().text().replaceAll("[\\D&&[^.]&&[^-]]+", "")));
}
public void setUpPanelsData(Connection.Response res) {
MacroData macroData = MacroData.getInstance();
TrafficData trafficData = TrafficData.getInstance();
PanelsData panelsData = PanelsData.getInstance();
String strona;
Document doc;
Elements elements;
int[] traffic = new int[8];
int[] macro = new int[7];
for (int i = 1; i < 8; i++) {
strona = "http://bannersbroker.com/adpubcombo_manageinventory/viewactivepanels?colour=" + i + "&sort=Qualified";
doc = getPage(res, strona);
elements = doc.select("span[id=limit2]");
macro[i - 1] = Integer.parseInt((elements.first().text().replaceAll("\\D+", "")));
elements = doc.select("div.number[id=js-tp-bank]");
traffic[i - 1] = Integer.parseInt((elements.first().text().replaceAll("\\D+", "")));
elements = doc.select("p[id^=QualDate]");
if (elements.hasText()) {
String[] luzem = elements.text().split("[\\D&&[^-]]+");
Date[] aray = new Date[luzem.length / 4];
try {
int j = 0;
while (elements.eq(j).hasText()) {
if (elements.eq(j).text() != "")
aray[j] = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(elements.eq(j).text());
j++;
}
j = 0;
} catch (ParseException e) {
e.printStackTrace();
}
switch (i) {
case 1:
panelsData.setYellowDays(aray);
break;
case 2:
panelsData.setPurpleDays(aray);
break;
case 3:
panelsData.setBlueDays(aray);
break;
case 4:
panelsData.setGreenDays(aray);
break;
case 5:
panelsData.setRedDays(aray);
break;
case 6:
panelsData.setBlackDays(aray);
break;
case 7:
panelsData.setPrestigeDays(aray);
break;
default:
break;
}
}
elements = getPanelData(doc);
if (elements.hasText()) {
int size = getPanelData(doc).size();
for (int j = 1; j <= size / 2; j++) elements.remove(j);
String[] luzem = elements.text().split("[\\D&&[^.]]+");
Double[] aray = new Double[luzem.length];
for (int j = 0; j < luzem.length; j++)
aray[j] = Double.parseDouble(luzem[j]);
switch (i) {
case 1:
panelsData.setYellow(aray);
break;
case 2:
panelsData.setPurple(aray);
break;
case 3:
panelsData.setBlue(aray);
break;
case 4:
panelsData.setGreen(aray);
break;
case 5:
panelsData.setRed(aray);
break;
case 6:
panelsData.setBlack(aray);
break;
case 7:
panelsData.setPrestige(aray);
break;
default:
break;
}
}
}
doc = getPage(res, "http://bannersbroker.com/adpubcombo_trafficbank/managetraffic");
elements = doc.select("h4[id=trafficLeft_4]");
if (elements.first() != null)
traffic[7] = Integer.parseInt((elements.first().text().replaceAll("[\\D+]", "")));
trafficData.setTraffic(traffic);
macroData.setMacro(macro);
}
public static Elements getPanelData(Document doc) {
Elements elements = doc.select("span.cost");
return elements;
}
public void setUpPaymentData(Connection.Response res) {
Document doc;
Elements elements;
doc = getPage(res, "http://bannersbroker.com/adpubcombo_manageinventory/viewpackagespurchased");
elements = doc.select("tr > td[align=center][valign=middle] > div");
try {
PaymentData.getInstance().setAbonamentDate(new SimpleDateFormat("yyyy-MM-dd").parse(elements.last().text()));
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setUpTrafficPayment(Connection.Response res) {
Document doc;
Elements elements;
doc = getPage(res, "https://bannersbroker.com/adpubcombo_account/account");
elements = doc.select("div[class=infogroup] > div[class=tablecont] > table > tbody > tr > td[align=center]:not(td[class])");
for (int i = 0; i < 5; i++) elements.remove(0);
int length = elements.size();
for (int i = 0; i < length / 3; i++) {
elements.remove(i);
elements.remove(i);
}
PaymentData.getInstance().setTpAmount(elements.size());
Date[] array = new Date[elements.size()];
for (int i = 0; i < elements.size(); i++)
try {
array[i] = new SimpleDateFormat("yyyy-MM-dd").parse(elements.eq(i).text());
} catch (ParseException e) {
e.printStackTrace();
}
PaymentData.getInstance().setTpDate(array);
}
}
构造函数获取电子邮件,密码和框架,在这种情况下框架并不重要所以我从main方法发送null,当我想从框架发送电子邮件和密码时它得到框架但是每次调试时都不方便这样做就是跳过登录窗口。
public JsoupClass(String emailText, String passText, MainFrame mainFrame) {
Connection.Response res;
res = getLoginData();
res = doLogin(res, emailText, passText);
if (checkLogin(res).hasText()) {
setUpTrafficPayment(res);
setUpEwalletData(res);
setUpPanelsData(res);
setUpPaymentData(res);
try {
if (mainFrame != null)
mainFrame.dispose();
new StatisticFrame();
} catch (ParseException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Wrong Login Data!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
}
getLoginData()
方法解析cookie和登录所需的一些隐藏字段
doLogin(res, emailText, passText)
使用从getLoginData()
获取的数据,我们的电子邮件和密码来登录我们。下一步是if (checkLogin(res).hasText())
,checkLogin(res)
返回元素,如果elements.hasText
我们知道我们登录成功,否则没有。
然后,当我们登录时,我将所需的数据解析为挖掘类的方法:
setUpTrafficPayment(res);
setUpEwalletData(res);
setUpPanelsData(res);
setUpPaymentData(res);
正如我在开始时所说的那样工作,突然间它每次都开始抛出异常。
Banners Broker是一个私人市场,注册仅在此时通过推荐。 如果enyone想要创建一个帐户并尝试登录,则需要一个链接进行注册。 https://bannersbroker.com/vardius
答案 0 :(得分:3)
您似乎致电checkLogin(null)
。
在调用res = doLogin(res, emailText, passText);
之前,您可以确保checkLogin
不为空。
尝试使用:
if (res != null && checkLogin(res).hasText()) {
捕捉异常(在您的情况下为IOException
)并且不执行任何操作(doLogin
和getLoginData
)的不良做法。您可以抛出异常或添加日志,但忽略它可能会在其他地方伤害您。这就是这里发生的......
更改此代码:
} catch (IOException ioe) {
} catch (IllegalArgumentException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);
对此:
} catch (IOException ioe) {
// Here print the ioe stack trace to whenever you want and check why you get this exception.
} catch (IllegalArgumentException ioe) {
JOptionPane.showMessageDialog(RunFrame.getInstance(),
"Could not open connection to Banners Broker!",
"Error",
JOptionPane.ERROR_MESSAGE);