I am trying to understand how reflection works, i cant able to figure out the problem. In the second for loop method.length is not fetching method from another program.
Following is the code:
public class DriverScript
{
public static ActionKeywords actionKeywords;
public static String sActionKeyword;
public static Method method[];
public DriverScript() throws NoSuchMethodException, SecurityException
{
actionKeywords = new ActionKeywords();
method = actionKeywords.getClass().getMethods();
}
public static void main(String[] args) throws Exception
{
System.out.println(method);
String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx";
ExcelUtils.setExcelFile(sPath, "Test Steps");
for (int iRow = 1; iRow <= 9; iRow++)
{
sActionKeyword = ExcelUtils.getCellData(iRow, 3);
execute_Actions();
}
}
private static void execute_Actions() throws Exception
{
for (int i = 0; i < 11; i++)
{
if (method[i].getName().equals(sActionKeyword))
{
method[i].invoke(actionKeywords);
break;
}
}
}
}
public class Test2 {
//Reflection in Keyword driven Framework
/**
* #authors Venkadesh,Selvakumar work name : Test
**/
static Class Class1 = ActionKeywords.class;
static ActionKeywords Keywords = new ActionKeywords();
public static void main(String[] args) throws Exception {
String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx";
ExcelUtils.setExcelFile(sPath, "Test Steps");
Method[] methods = Class1.getDeclaredMethods();
for (int i = 1; i <= 9; i++) {
// This to get the value of column Action Keyword from the excel
String sActionKeyword = ExcelUtils.getCellData(i, 3);
for (int j = 1; j <= 9; j++) {
if (methods[j].getName().equals(sActionKeyword)) {
try {
methods[j].invoke(Keywords);
} catch (Exception e) {
}
System.out.println(methods[j]);
}
}
}
}
}
try with this it works
You are not calling the constructor for DriverScript so the fields are not initialized.
Try changing your methods from static to non-static and fix from there.
Replace
public static Method method[];
with
public static Method method[]= ActionKeywords.class.getDeclaredMethods();
Related
I have this 2 classes:
public class Sumador {
public static void main(String[] args) {
int primerNumero = Integer.parseInt(args[0]);
int segundoNumero = Integer.parseInt(args[1]);
int suma = 0;
for(int k=primerNumero; k < segundoNumero; k++) {
suma += k;
}
System.out.println("Resultado: " + suma);
}
}
public class Lanzador {
public void lanzarSumador() {
Process p;
InputStream is;
BufferedReader br = null;
try {
p = new ProcessBuilder("java", "Sumador.java", "10", "20").start();
is = p.getInputStream();
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while(br.ready()) {
System.out.println(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Lanzador l = new Lanzador();
l.lanzarSumador();
}
}
What i want to do is running the second class to get the first one as a process in the second one, but unfortunetly for me, it doesn't work and i can't understand why.
Every reply would be really thankful. Thank you for your time.
EDIT:
You can call the main method of class Sumador from the main method in class Lanzador like show next:
public static void main(String[] args) {
Lanzador l = new Lanzador();
l.lanzarSumador();
Sumador.main(null);
}
You only can have one main class in your program execution. What you can do is change the main method in class Sumador at a public class and call it in the main method of class Lanzador
public class Sumador {
public static void sumadorMethod() {
int primerNumero = Integer.parseInt(args[0]);
int segundoNumero = Integer.parseInt(args[1]);
int suma = 0;
for(int k=primerNumero; k < segundoNumero; k++) {
suma += k;
}
System.out.println("Resultado: " + suma);
}
}
Then you main method in class Lanzador look like this:
public static void main(String[] args) {
Lanzador l = new Lanzador();
l.lanzarSumador();
Sumador.sumadorMethod();
}
Cannot make a reference to the nonstatic method toString() other is saying that I can't have a static toString but when I change it and go to the main it says I need a static toString. I was wondering if anyone could make sense of it because I referenced multiple codes and notes from classes but could not understand.
import java.util.Scanner;
import java.util.Arrays;
class Main
{
public static void main(String[] args)
{
String[] answer = new String();
answerArray();
Quiz Quiz1 = new Quiz();
Quiz.toString();
System.out.print("Number of Correct Answers: ");
System.out.println(Quiz.totalCorrect(answerArray[]));
System.out.print("Number of Mistakes: ");
System.out.println(Quiz.totalMistakes(answerArray[]));
System.out.println("The Student is"+Quiz.isPassing());
}
public static String answerArray()
{
Scanner scan = new Scanner(System.in);
String[] answerArray = new String[10];
for (int i = 0; i < answerArray.length; i++)
{
System.out.println("Enter Grade "+i+1);
answerArray[i]=scan.next;
answerArray[i]=answerArray[i].toLowerCase();
}
return answerArray;
}
}
Quiz Class:
import java.util.Arrays;
class Quiz
{
public static void main(String[] args) {
String[] answerKey = new String[10];
String[] studentAns = new String[answerKey.length];
answerKey[0] = B;
answerKey[1] = D;
answerKey[2] = C;
answerKey[3] = A;
answerKey[4] = E;
answerKey[5] = A;
answerKey[6] = B;
answerKey[7] = A;
answerKey[8] = E;
answerKey[9] = B;
}
public static void studentAns(String answerKey[])
{
for (int i = 0; i < answerArray.length; i++)
studentAns[i]=answerKey[i];
for (int i=0; i<answerKey.length;i++)
studentAns[i]=studentAns[i].toUpperCase();
}
public static String getstudentAns()
{
return studentAns;
}
public static int totalCorrect(String answerArray[])
{
int numCorrect=0;
for (int i = 0; i < answerArray.length; i++)
{
if (answerArray[i].equals(studentAns[i]))
numCorrect=numCorrect+1;
}
return numCorrect;
}
public static int totalMistakes(String answerArray[])
{
int numMistakes=0;
for (int i = 0; i < answerKey.length; i++)
{
if (answerArray[i]!=studentAns[i])
numMistakes=numMistakes+1;
}
return numMistakes;
}
public static boolean isPassing()
{
if (totalCorrect-numMistakes>=7)
return PASSED;
else
return FAILED;
}
public static String toString(String answerArray[])
{
String firstLine = "ANSWER KEY \t Student's Answers";
for (int i = 0; i < answerKey.length; i++)
{
String full=(i+1)+") "+answerArray[i]+"\t\t"+(i+1)+studentAns[i]+"\n";
return full;
}
}
}
Here you are trying to use a static method toString() but that does not exist because classes only have the nonstatic method toString().
Quiz.toString();
To use your static method you should write this instead:
Quiz.toString(answerArray());
I would like to know the reason for fragile base class problem in java.
Here is the code and i cant find the reason for triggering child class method even if i have given instruction to trigger a method in parent class
in public class Array
public class Array {
private ArrayList<Object> a = new ArrayList<Object>();
public void add(Object element)
{
a.add(element);
}
public void addAll(Object elements[])
{
for (int i = 0; i < elements.length; ++i)
add(elements[i]);
}
}
in ArrayCount class... this class is responsible for store the count of the list
public class ArrayCount extends Array {
public int count = 0;
#Override
public void add(Object element)
{
super.add(element);
++count;
}
#Override
public void addAll(Object elements[])
{
super.addAll(elements);
count += elements.length;
}
}
main class as follows
public class Main {
public static void main(String[] args) {
ArrayCount ac = new ArrayCount();
Object [] objArray = new Object[7];
objArray[0] = "xxx";
objArray[1] = "xxx";
objArray[2] = "xxx";
objArray[3] = "xxx";
objArray[4] = "xxx";
objArray[5] = "xxx";
objArray[6] = "xxx";
ac.addAll(objArray);
System.out.println("count is : " + ac.count);
//COUNT IS 14 NOT 7
}
}
The code below compiled successfully here https://www.compilejava.net/ but execution fails
Error: Could not find or load main class ClassDemo
whereas it does have a main entry point. Why ?
package com.tutorialspoint;
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
try {
ClassDemo c = new ClassDemo();
Class cls = c.getClass();
// returns the array of Field objects
Field[] fields = cls.getDeclaredFields();
for(int i = 0; i < fields.length; i++) {
System.out.println("Field = " + fields[i].toString());
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public ClassDemo() {
// no argument constructor
}
public ClassDemo(long l, int i) {
this.l = l;
this.i = i;
}
long l = 77688;
int i = 3;
}
You need to remove the package identifier from your code (first line) since you are using an online compiler/executor.
Hope this helps.
It's because you have a package statement.
Remove that and it will work just fine.
Our professor assigns us exercises through the jarpeb sytsem (Java Randomised and Personalised Exercise Builder). So the variables names are random.
public class Eczema extends Thread {
private int aurite;
private int[] serlvulate;
public Eczema(int[] serlvulate) {
this.serlvulate = serlvulate;
}
public int getAurite () {
return aurite;
}
#Override
public void run () {
try {
aurite = Integer.MAX_VALUE;
for (int i = 0; i < serlvulate.length; i++) {
if (i < this.aurite) {
this.aurite = i;
sleep (1000);
}
}
} catch (InterruptedException e) {
System.out.println("Found an Exception!");
return;
}
}
}
public class Stub {
public static int polytoky (int[]a, int[]b) throws InterruptedException {
Eczema Eczema1 = new Eczema (a);
Eczema Eczema2 = new Eczema (b);
Eczema1.start();
Eczema1.join();
Eczema2.start();
Eczema2.join();
return Math.min (Eczema1.getAurite(), Eczema2.getAurite());
}
}
I followed the instructions of the exercise but when I chech it on the cmd the following error occurs:
Field servulate not found: java.lang.NoSuchFieldException: servulate.
Any ideas how I fix it?
You appear to be accessing a field named servulate while the proper field name is serlvulate. Find the line where this happens and fix the spelling.