Johnatan56
Honorary Master
Hi,
I am currently writing an app for class whereby I am trying to send two arrays to a second class in order to display them in a table (each has a column using TableLayout).
So when you click the button, two ArrayLists are created that need to be sent over. So far I am getting a NullPointerException on line 24, which is when I try to get the intent from the other class. I am still new to intents (learnt about them today in class), so any advice is appreciated.
(It is a subnet calculator)
The button on the main screen:
The code for the second class:
Thanks for any help.
EDIT: If I comment out the try/catch so it only allows the getIntent, it is a NullPointerException at line 24 in NetworkSecond. I am assuming there is an issue with the sending of the intent?
Thanks.
I am currently writing an app for class whereby I am trying to send two arrays to a second class in order to display them in a table (each has a column using TableLayout).
So when you click the button, two ArrayLists are created that need to be sent over. So far I am getting a NullPointerException on line 24, which is when I try to get the intent from the other class. I am still new to intents (learnt about them today in class), so any advice is appreciated.
(It is a subnet calculator)
The button on the main screen:
Code:
public void sendNetworkSecond(View v){
Intent intent = new Intent(this, NetworkSecond.class);
final EditText subnetSlash = (EditText)findViewById(R.id.slashSubnet);
try {
subSlash = Integer.parseInt(subnetSlash.getText().toString());
} catch (NumberFormatException e) {
subSlash = 24; //Default subnet.
}
//Subnet calculation suggestion for class C.
if(subSlash > 24){
x = 0;
y = 0;
x = subSlash / 8;
y = (subSlash - x*8);
networkPortion = y;
}
//Subnet calculation suggestion for class B
if(subSlash > 16 && subSlash <24){
x = 0;
y = 0;
x = subSlash / 8;
y = (subSlash - x*8);
networkPortion = y;
}
//Calculate the range.
if(networkPortion != 0){
//Calculate the number of networks.
amountNetworks = (int) Math.pow(2.0,(double)(networkPortion)) - 2;
//Calculate number of hosts.
amountHosts = (x + 1) * 8 - amountNetworks;
//Calculate the range.
range = 256 / amountNetworks;
}
//Calculate network address and broadcast.
if(range != 0){
int total = 0;
while(total < 255) {
networkAddress.add(total);
broadcastAddress.add(total + range - 1);
total += range;
}
}
//Send over to next screen. Make sure that there is a range, else there would be nothing to send.
if(range!= 0){
intent.putIntegerArrayListExtra("networkAddress", networkAddress);
intent.putIntegerArrayListExtra("broadcastAddress", broadcastAddress);
}
startActivity(intent);
}
The code for the second class:
Code:
public class NetworkSecond extends Activity{
Intent intent = getIntent();
ArrayList<Integer> networkAddress = new ArrayList<>();
ArrayList<Integer> broadcastAddress = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_network_second);
try{
networkAddress = intent.getIntegerArrayListExtra("networkAddress");
broadcastAddress = intent.getIntegerArrayListExtra("broadcastAddress");
}catch(Exception e){
//In case the Array is empty.
networkAddress.add(0);
broadcastAddress.add(0);
}
//Need to use the table to display the two arrays. One array per row.
TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
for(int i = 0; i < networkAddress.size(); i++){
TableRow row = new TableRow(this);
int subnetID = networkAddress.get(i);
int broadcast = broadcastAddress.get(i);
TextView subnetting = new TextView(this);
subnetting.setText("" + subnetID);
TextView broadcasting = new TextView(this);
broadcasting.setText("-" + broadcast);
row.addView(subnetting);
row.addView(broadcasting);
table.addView(row);
}
}
}
Thanks for any help.
EDIT: If I comment out the try/catch so it only allows the getIntent, it is a NullPointerException at line 24 in NetworkSecond. I am assuming there is an issue with the sending of the intent?
Thanks.
