0
votes

So I tried to make a port scanner and I got stuck on the way and i will love to get some explication of what am I doing wrong here(and almost every time) when I am starting a new activity.

P.S. :4rd time programing with java in my life.

My java:

package com.nliplace.nli.xxxx;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class portscan extends AppCompatActivity {
    EditText Portaa  = (EditText)findViewById(R.id.editText3);
    EditText Portbb = (EditText)findViewById(R.id.editText4);
    EditText Iptot = (EditText)findViewById(R.id.editText5);
    TextView rez = (TextView)findViewById(R.id.textView18);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_portscan);

    }
    private static String available(String ip, int porta, int portb) {
        String rezultat = "";
        for (int port = porta; port <= portb; port++) {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("185.28.20.123", port), 1000);
                socket.close();
                rezultat = rezultat + "," + port;

            } catch (Exception ex) {
            }

        }
        return rezultat;
    }

    public void scanclick(View s) {
        // Scaneaza
        Iptot= (EditText)this.findViewById(R.id.editText3);
        Portaa = (EditText)this.findViewById(R.id.editText4);
        Portbb = (EditText)this.findViewById(R.id.editText5);
        String portaa = Portaa.getText().toString();
        int porta = Integer.parseInt(portaa);
        String portbb = Portbb.getText().toString();
        int portb = Integer.parseInt(portbb);
        String ip = Iptot.getText().toString();
        // available(ip, porta, portb);
        //textView18
        rez.setText(available(ip, porta, portb));
    }


}

My XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.nliplace.nli.unealta.portscan">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Aceasta activitate scaneaza dupa port-uri deschise."
            android:id="@+id/textView13"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Adresa:"
            android:id="@+id/textView14" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText3"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Port A:"
            android:id="@+id/textView15" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Port B:"
            android:id="@+id/textView16" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scan"
            android:id="@+id/button11"
            android:layout_gravity="center_horizontal"
            android:onClick="scanclick" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Aici se vor afisa porturile deschise:"
            android:id="@+id/textView17"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="...va dura..."
            android:id="@+id/textView18"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</RelativeLayout>

My challenge for you guys:

FATAL EXCEPTION: main Process: com.nliplace.nli.unealta, PID: 20048 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nliplace.nli.unealta/com.nliplace.nli.unealta.portscan}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2551) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2172) at com.nliplace.nli.unealta.portscan.(portscan.java:20) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1690) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)  at android.app.ActivityThread.access$900(ActivityThread.java:177)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5910)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

1
very rude of you :) I want tips! no need to write all the code!Nitescu Lucian
and I just need someone to say/scream at me at what am I doing wrong.Nitescu Lucian
oh ..s... now i see :))Nitescu Lucian
thank you for the explication :PI wrestled a bear once.

1 Answers

0
votes

You cant use findViewById() method before using setContentView() in onCreate(). Put find views inside onCreate() after setContentView() method like this:

public class portscan extends AppCompatActivity {
    EditText Portaa;
    EditText Portbb;
    EditText Iptot;
    TextView rez;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_portscan);
     Portaa  = (EditText)findViewById(R.id.editText3);
    Portbb = (EditText)findViewById(R.id.editText4);
    Iptot = (EditText)findViewById(R.id.editText5);
    rez = (TextView)findViewById(R.id.textView18);

    }...

You were basically trying to find something that doesnt exist.