简体   繁体   English

Java 类之间传递空字符串的问题

[英]Problem with Passing Null String Between Classes in Java

I'm trying to pass a string from one class to another, but instead of it passing the string I need, it's passing a null value, which causes an error in the decryption function I'm trying to do.我试图将一个字符串从一个类传递到另一个类,但它不是传递我需要的字符串,而是传递一个空值,这会导致我尝试执行的解密函数出错。 Here are the two classes with details on each one that I'm trying to achieve:以下是我试图实现的两个类,其中包含每个类的详细信息:

MainActivity -> trying to take the value I get from "String decrypted1 = d1.encryppass(encrypted);" MainActivity -> 试图获取我从“字符串解密1 = d1.encryppass(加密);”中获得的值

                    Decrypt d1 = new Decrypt();
                try {
                    if (cursor.moveToFirst()) {
                        do {
                            String name = cursor.getString(0);
                            String encrypted = cursor.getString(1);
                            d1.myMethod(this);
                            decrypted1 = d1.encryppass(encrypted);
                            Log.i("Title:     ", name);
                            Log.i("Password:  ", decrypted1);
                            Toast.makeText(getApplicationContext(), decrypted1, Toast.LENGTH_LONG).show();
                        } while (cursor.moveToNext());
                    }
                } catch (NullPointerException exception) {
                    exception.printStackTrace();
                }
            }

Decryption 2 class -> and pass that above statement here "private String pinToMd5 = d3.decrypted1;"解密 2 类 -> 并在此处传递上述语句“private String pinToMd5 = d3.decrypted1;”

public class Decrypt2 {
    private Cipher cipher;
    MainActivity d3;
    private String pinToMd5;
    public String pin_to_md5;SecretKeySpec secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), "AES");

    public Decrypt2(MainActivity mainActivity)
        {
        d3 = mainActivity;
        pinToMd5 = d3.decrypted1;
        pin_to_md5 = MD5(pinToMd5);
        }

From what I can see the issue might be here从我可以看到的问题可能在这里

MainActivity d3 = new MainActivity();
    //private String pinToMd5 = "PaSsW0rD"; //use this to test my code to confirm any changes doesn't break the logic -> this works
    private String pinToMd5 = d3.decrypted1;

You are creating new instance from MainActivity class and expect the object from the one presented in the stack.您正在从 MainActivity 类创建新实例,并期望堆栈中显示的对象中的对象。 Try passing the context or reference from the existing main activity class and assign it in the Decrypt class尝试从现有的主活动类传递上下文或引用,并在 Decrypt 类中分配它

    InputStream instream = getContentResolver().openInputStream(singleUri);
/*Reference from MainActivity class*/
                        Decrypt2 d2 = new Decrypt2(this);
                        String decrypted2 = d2.encrypnote(instream);

public class Decrypt2 {
//your code...

MainActivity d3;
    //private String pinToMd5 = "PaSsW0rD"; //use this to test my code to confirm any changes doesn't break the logic -> this works
private String pinToMd5;

public Decrypt2(MainActivity mainActivity)
{
    d3 = mainActivity;
    pinToMd5 = d3.decrypted1;
}
//your code...
}

Not sure this will fix your problem completely :)不确定这会完全解决您的问题:)

Second issue is this code here:第二个问题是这里的代码:

public String pin_to_md5 = MD5(pinToMd5);公共字符串 pin_to_md5 = MD5(pinToMd5);

You are invoking the method MD5 before the pinToMd5 property is initialised?您是在 pinToMd5 属性初始化之前调用方法 MD5 吗? when did you need to use that class?你什么时候需要使用那个类? If on the initialization of the class than do this changes in the code:如果在类的初始化上进行代码更改:

public class Decrypt2 {
//your code...

MainActivity d3;
    //private String pinToMd5 = "PaSsW0rD"; //use this to test my code to confirm any changes doesn't break the logic -> this works
private String pinToMd5;
//remove MD5(pinToMd5);
public String pin_to_md5;
SecretKeySpec secretKeySpe;


public Decrypt2(MainActivity mainActivity)
{
    d3 = mainActivity;
    pinToMd5 = d3.decrypted1;
    pin_to_md5 = MD5(pinToMd5);
    secretKeySpe = new SecretKeySpec(this.pin_to_md5.getBytes(), "AES");

}
//your code...
}

Third issue In MainActivity you are using第三个问题在您使用的 MainActivity 中

public String decrypted1 = "";

as property.作为财产。 But then in onClick method you are creating local variable that contains the encrypted string但是在 onClick 方法中,您正在创建包含加密字符串的局部变量

     d1.myMethod(this);
//this code here
     String decrypted1 = d1.encryppass(encrypted);
    
     Log.i("Title:     ", name);

Local values live only inside the method but not in the class so instead creating new variable in the method use the one from above.局部值只存在于方法内部,而不存在于类中,因此在方法中创建新变量使用上面的变量。 Remove that String .. and make the code smth like this删除那个 String .. 并使代码变得像这样

d1.myMethod(this);
//this code here
decrypted1 = d1.encryppass(encrypted);
        
Log.i("Title:     ", name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM