とPHPエコー出力を読んだとき、私はエラーのJava
java.lang.NullPointerExceptionが私のアプリでは、次のエラーを取得しています:仮想メソッドを呼び出そうと「はjava.lang.String型java.lang.Object.toString ()」HTTP接続は次のようになりますが開始されたヌルオブジェクト参照
JAVAコード上:
boolean l = true;
String[] ergebnisArray = new String[100];
for(int i = 0; i < ergebnisArray.length; i++) {
ergebnisArray[i] = "";
}
String count = "0";
if(l) {
try {
String login_url = "http://192.168.178.82:8080/webapp/test.php";
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data = URLEncoder.encode("count", "UTF-8") + "=" + URLEncoder.encode(count, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
if (response != "") {
int c = Integer.parseInt(count);
ergebnisArray[c] = response;
c++;
count = Integer.toString(c);
} else {
l = false;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return ergebnisArray;
そして最後にPHPコード:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$zaehler = $_POST["count"];
$sql = "SELECT * FROM stundenplan";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
for ($i = 0; $i <= (int)$zaehler; $i++) {
$output = "".$row["Lehrer"]." ".$row["Klasse"]." ".$row["Stunde"];
}
}
} else {
echo "";
}
echo $output;
$conn->close();
?>
あなたは、関数の外で$出力を宣言する必要があり、事前
'response!=" "'このようにStringオブジェクトを比較しないでください。 'String.equals'を使うか、' length == 0'を確認するだけです – copeg
'java.lang.NullPointerException'この例外はどこに投げられますか? – copeg